Depending on the type of x, y and z you may have to be careful to store a copy into the tuple.
(Also, your indentation is wrong, and there is no such thing as end in python.)
Update: Ok, if I understand better, the question is just to be able to use the previous value the next time through. In the simplest case, there is no problem at all: the next time through a loop, the the value of x,y, and z are whatever they were at the end of the previous time through the loop (this is the way all programming languages work).
Python is a great language for many tasks. It is commonly used for system administration tasks, as well as building websites, processing data, and text. It is also shaping up to be the language of choice for Machine Learning (ML), leveraging powerful modules for performing math and for visualizations.
As with the basis of most programming languages, you may use variables in Python to hold and manipulate values. This guide shows you the basics of creation and use of variables in Python.
To best benefit from this guide, you may want to follow along and run the code examples throughout this guide. The code examples are entered into Python's REPL interpreter. If your system does not already have a python interpreter, you can download one from here. Just pick the version matching your operating system and follow the installation instructions. This guide targets Python version 3.6 and the sample code was tested against that version.
Variables
Variables hold values. In Python, variables do not require forward declaration - all you need to do is provide a variable name and assign it some value.
The Python interpreter shows you a prompt that looks like this: 1>>> one 21 3>>>9. Each line you type into the interpreter is taken one at a time, parsed by the interpreter, and if the line is complete, executed as well.
If you enter 1>>> print(one) 21 3>>>0 in the Python interpreter and hit "Enter", the interpreter will just show you a new line prompt.
1>>> one = 1 2>>>
python
The new line prompt 1>>> one 21 3>>>9 is empty. But Python actually did a few things:
- A variable named 1>>> print(one) 21 3>>>2 was created.
- The value 1>>> print(one) 21 3>>>3 was assigned to the variable 1>>> print(one) 21 3>>>2.
This is not apparent from the blank line output. But the interpreter can show you the value of any variable if you just type the variable name and hit enter:
1>>> one 21 3>>>
python
The value 1 is shown because Python evaluates the line and reports the value returned. Previously, the line contained a statement. The variable 1>>> print(one) 21 3>>>2 was assigned a value. That operation evaluated a statement, so nothing was printed as a result. A more explicit way to print the value of a variable is to use the 1>>> print(one) 21 3>>>6 function.
1>>> print(one) 21 3>>>
python
Let's create another variable named 1>>> print(one) 21 3>>>7 and assign it the value 1>>> print(one) 21 3>>>8:
1>>> greeting = 'hi' 2>>> print(greeting) 3hi
python
Here we created a variable and assigned it a string value. Note the variable name 1>>> print(one) 21 3>>>7. It was chosen to contain, well, a greeting of some sort. Python, of course, has no way to tell that the string value 1>>> greeting = 'hi' 2>>> print(greeting) 3hi0 is indeed a greeting. A variable is, well, variable! We can re-assign a variable later. The value stored in a variable is simply the last one assigned to it.
1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>
python
The initial value 1>>> greeting = 'hi' 2>>> print(greeting) 3hi1 is lost once the second assignment to the value 1>>> greeting = 'hi' 2>>> print(greeting) 3hi2 was evaluated. The current value of the variable remains 1>>> greeting = 'hi' 2>>> print(greeting) 3hi3 for the duration of the session unless otherwise assigned a new value later.
Both variable names 1>>> greeting = 'hi' 2>>> print(greeting) 3hi4 and 1>>> print(one) 21 3>>>7 consist of characters only. Python allows you to name variables to your liking, as long as the names follow these rules:
- Variable names may contain letters, digits (0-9) or the underscore character 1>>> greeting = 'hi' 2>>> print(greeting) 3hi6.
- Variable names must begin with a letter from A-Z or the underscore 1>>> greeting = 'hi' 2>>> print(greeting) 3hi6 character. Either lowercase or uppercase letters are acceptable.
- Variable names may not be a reserved word in Python.
Following the rules above, all of these variable assignments are legal:
1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'
python
All the above variable names are acceptable. But just because they are acceptable does not mean you should use them. The Python community has further developed naming conventions which should be followed. For example, even though a single-character identifier is perfectly legal, you are strongly discouraged from using the characters 1>>> greeting = 'hi' 2>>> print(greeting) 3hi8 (lower case el) or 1>>> greeting = 'hi' 2>>> print(greeting) 3hi9 (uppercase oh) or 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>0 (uppercase eye). This is because in some fonts these are hard to distinguish from the digits 1 (one) and 0 (zero). For more on variable naming, .
The following variable names are not acceptable. If you attempt to use them, python will produce an error and no variable would be created.
An initial character which is not an underscore or a letter from A-Z or a-z will produce an error. The backtick (`) character for example:
1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>
python
An identifier starting with a digit is not legal.
1>>> 7days = 'week' 2 File "<stdin>", line 1 3 7days = 'week' 4 ^ 5SyntaxError: invalid syntax 6>>>
python
An identifier containing a space isn't legal:
1>>> day of week = 'Monday' 2 File "<stdin>", line 1 3 day of week = 'Monday' 4 ^ 5SyntaxError: invalid syntax 6>>>
python
Also, we can't use reserved words as variable names. In python, the word 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>1 is a reserved word. The following assignment will therefore fail:
1>>> and = 'the winner is?' 2 File "<stdin>", line 1 3 and = 'the winner is?' 4 ^ 5SyntaxError: invalid syntax 6>>>
python
In all of the failed cases above, the Python interpreter raised an error and refused to carry out the assignment or creation of the variable. You may note that the caret 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>2 character points to different position in the erroneous identifier. This is due to the interpreter's attempt to match the identifier to an acceptable syntax. But either way, the outcome is the same: invalid variable names result in an error.
As a reference, Python's reserved words list includes:
andasassertbreakclasscontinuedefdelelifelseexceptFalsefinallyforfromglobalifimportinislambdaNonenonlocalnotorpassraisereturnTruetrywhilewithyield
Variables and Type
Python does not require you to declare a variable. You do not need to tell Python ahead of time that you intend to reserve space for a variable. All you do is assign a variable a value. But this does not mean you can use a variable without having Python allocate it first. For example, the following line will fail in my session:
1>>> one 21 3>>>0
python
This error appears because there is no identifier named 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>3 as far as Python can tell. Python will joyfully accept a variable by that name, but it requires that any variable being used must already be assigned.
The act of assignment to a variable allocates the name and space for the variable to contain a value.
We saw that we can assign a variable a numeric value as well as a string (text) value. We also saw that we can re-assign a variable, providing it a new value which replaces any previous value it contained.
Python tracks the value of a variable by letting you access it via the variable name. Python also tracks the type of the value assigned to a variable. To tell what the value type is, you can use the built-in 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>4 function. In the following examples, we use the 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>4 function to display the value type:
1>>> one 21 3>>>1
python
In each of the examples above, Python infers the value type by parsing the right-hand part of the assignment and deciding the type accordingly. The existence of the decimal point in the value 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>6 clued Python to assign the type float whereas the bare number 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>7 produced an int.
Python also supports boolean data types. Booleans are assigned a value of 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>8 or 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>9 (both of which are keywords by the way).
An integer data type is also created when you use hexadecimal or octal or binary literals. To type a value as octal, prefix the number with 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'0. To type a value is hexadecimal, prefix it with 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'1. For a binary literal, prefix with 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'2.
1>>> one 21 3>>>2
python
If you want to ensure the value of a variable is of int type, you may use the built-in 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'3 class constructor:
1>>> one 21 3>>>3
python
The above statement assigned the variable type class int to 1>>> greeting = 'hi' 2>>> print(greeting) 3hi4. In order to store the number 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>6 to an integer value, the 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'3 function discarded the fraction part.
Similarly, you can use the 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'7 class constructor function to ensure that a bare number - expressed in decimal, hex, or octal forms - would yield a float data type:
1>>> one 21 3>>>4
python
Mixing Types
We saw that values do indeed have a type and that Python tracks variable value as well as type. Lastly though - what does this type mean? Python will allow you to perform operations that fit the type.
For example, you may wish to divide the value of a variable by 3:
1>>> one 21 3>>>5
python
But the division operator does not work on a string. So you can't divide the string 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'8 into 3:
1>>> one 21 3>>>6
python
The error Python raises is descriptive of the fact that the operator 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'9 (used for numeric division) is not defined for the types string and integer. Python is aware of the type assigned to the variable 1>>> greeting = 'hi' 2>>> print(greeting) 3hi4 which is how it made that determination. While you may be able to define your own operators on any types you wish, the point remains that Python does need the type system in order to map values, operators, and variables to the correct internal function. Python is a dynamically typed, but typed nonetheless.
The None Type
Many programming languages support the notion of 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>1. Null is treated as a special value denoting "not-a-value", something which would let us denote an "empty" or undefined value. Python's version of that is the keyword 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>2, which is backed by the class 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>3. Note that assigning a variable to 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>2 does not get rid of the variable. Space is still allocated for the variable - only the value is set to 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>2. If you want to remove the variable altogether you may use the 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>6 statement:
1>>> one 21 3>>>7
python
In the example above, after deleting the variable, any attempt to use that variable produces an error stating it is not (or no longer) defined.
Checking for Type Equality
While the 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>4 function lets us glean which type a variable contains. When comparing numbers, we may need to check that they are identical - that both their value and type match. The 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>8 operator provides for such identity checking. Numeric values may compare as equal to each other using the equality test 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>9 yet not match on their type. Consider this example:
1>>> one 21 3>>>8
python
In the above example, 1>>> greeting = 'hi' 2>>> print(greeting) 3hi4 is assigned the integer value 1>>> print(one) 21 3>>>3 and 1>>> 7days = 'week' 2 File "<stdin>", line 1 3 7days = 'week' 4 ^ 5SyntaxError: invalid syntax 6>>>2 is assigned the float value 1>>> 7days = 'week' 2 File "<stdin>", line 1 3 7days = 'week' 4 ^ 5SyntaxError: invalid syntax 6>>>3. When tested using the equality match 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>9, the result is 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>8. Yet when tested using the object identity operator 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>8, the result is 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>9 since float and int are different types.
Python does let you define your own operators on your objects, so you could add support for both the equality and the identity operators on your classes. The default behavior of most non-numeric classes though is that two instances of an object would not evaluate as equal or identical to each other.
Strings are a bit different. Strings in Python are immutable reference types. To complicate thing more, two strings containing the same exact sequence of characters and compared for object identity may produce either 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>8 or 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>9.This is due to internal implementation details and may vary across Python interpreters.
Summary
To summarize: Python lets you create variables simply by assigning a value to the variable, without the need to declare the variable upfront. The value assigned to a variable determines the variable type. Different types may support some operations which others don't. If you want to control the type of variable assigned, you may use the specific class constructor to assign the value, such as 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'3 or 1>>> fish = 11 2>>> a = 12 3>>> _wow_ = 13 4>>> a1 = 14 5>>> something_longer_is_fine_2 = 15 6>>> RemoteAddress = '10.20.30.40'7. Bare numbers expressed without a decimal point - or as hex or octal literals - will produce an integer. You can get the class type of a variable by using the 1>>> greeting = 'hi once' 2>>> greeting = 'hi again!' 3>>> print(greeting) 4hi again! 5>>>4 function, or test whether a type matches some specific type using the 1>>> `ticked = 1 2 File "<stdin>", line 1 3 `ticked = 1 4 ^ 5SyntaxError: invalid syntax 6>>>8 operator.
Python variables provide a simple and dynamic way to create variables, yet maintains a powerful type system to ensure safe operations on your data.