This is number 5 in the series of posts we have put together introducing Python as a development language. Feel free to head back to our previous posts to get started with some of the more basics in the Python language:
@run.vince.run/manipulating-text-files-and-strings-in-beginner-python
@run.vince.run/running-your-python-code
Python has no specific command for declaring a variable, with the variable being created at the moment you assign a value to it. We assign the value to the variable using a single equal sign(=) with the variable name being equal to the value assigned on the right side of the equal sign.
In the example below, we create the variable named variable_name and assign it a string value of "variable value".
variable_name = "variable value"
The name of a variable is usually something descriptive of what is being stored in the variable, for example:
>>> cats_name = "Rusty"
>>> cats_age = "two"
To change the value of the variable, all we need to do is reassign the value using the equal sign again. For example, if our cat is one year older, we can change the value as we have below. You will also notice that we first stored a string in the variable named cats_age, we then decided to change this to an integer, and Python allows us to do this without any errors, it will know what type of data is being stored without the programmer needing to let it know.
>>> cats_age = "three" # this is a string
>>> cats_age = 3 # this is an integer
We can then use these variables in our code like the print() function we have used below. As you can see before we print out a statement using our variables, we can use the type() function to see what type of data the variables are holding:
>>> print(type(cats_name))
<class 'str'>
>>> print(type(cats_age))
<class 'int'>
>>> print("My cats name is {} and he is {} years old".format(cats_name, cats_age))
My cats name is Rusty and he is 3 years old
As we mentioned earlier, variable names should be descriptive but there will be situations when you will try to limit the amount of characters you are typing with your variable name only being a single character like an x or a y. This is valid in Python, with the only rules for variable names being:
Our variables will be removed as soon as our program stops as it is stored in in your systems RAM. Another way to make sure we can hold onto variables for longer is by using files again as we did in our last post. You'll notice one thing though, when we use the f.write() function to write our variable to a file, we need to enclose our variable name in the str() function. This is because we can only write strings to files. The str() function can be used to change an interger to a string, and the int() function can be used to change a string to an integer.
>>> my_savings = 500
>>> f = open("savings.txt","w")
>>> f.write(str(my_savings))
>>> f.close()
Now getting the data back into a variable, we can use the following:
>>> f = open("savings.txt", "r")
>>> savings_from_file = f.read()
>>> f.close()
>>> print("I have saved ${} so far this year".format(savings_from_file)
I have saved $500 so far this year
>>> print(type(5))
<class 'int'>
>>> print(type("5"))
<class 'str'>
Numeric data types aren't too complex but there are a few things you need to keep in mind when you are using them.
>>> print(5)
5
>>> print(-5)
-5
>>> print(+5)
5
>>> type(5.4)
<class 'float'>
>>> print(0.000000000000000000000000456)
4.56e-25
Operator precedence follows the usual flow where:
echo "" > check_savings.py
1 #!/usr/bin/env python
2
3 f = open("savings.txt", "r")
4 savings_from_file = f.read()
5 f.close()
6
7 print("I have saved ${} so far this year".format(savings_from_file))
python3 check_savings.py
I have saved $500 so far this year
If you've been following from the start, we took our time to set things up but now we are moving a lot quicker through our code and hopefully introducing you to a lot of new functionality. We should be able in another week with some more progress on getting user input and improving our programs further.