Variables are the starting point for programming language, as the programs need to store data into variables. Any programming language need to process data such as numbers, strings, characters, etc. The data can be used directly or saved in variables for future usage.
A variable can be thought of a small box which used to keep the data in program. Each variable has a unique name and you can find the data in the variable by calling its name.
Opposite of variables are constants. Both of variables or constants are small boxes used to “store” data. The difference between variable and constant is the data saved by a variable can be modified multiple times. However, as for constant, once it is saved, then it cannot be modified.
The python variable has name rules. It must begin with an alphabet or underscore. The python variable name must consist of numbers, alphabets and underscores. No spaces are allowed in variable name. It is not advised to use reserved words such as del, for, is, raise, assert, etc. as variable names.
In programming languages, the process of storing data into a variable is called assignment. Python usually uses the equal sign “=” as an assignment operation, the format is:
name = value
while as:
name: variable name
value: value (which is the data to be stored)
Note that variable is a type of identifier and it cannot be named randomly. Thus, it is important to follow the Python identifier naming requirement and avoid same names with Python’s built-in functions.
For example: The following statement assigns the integer 10 to the variable n:
n = 10 #assign integer 10 to variable n
From now on, n represents the integer 10 and using letter “n” same as using 10.
More examples:
a = 123 #assign integer 123 to variable n b = "Welcome to Free Learning Points"
The value of a variable is not fixed. It can be modified at any time if you reassign it. In addition, you don’t need to care about the type of data. You can assign different types of data to the same variable. You can refer to the following example:
n = 10 #Assign 10 to the variable n n = 27 #Assign 27 to the variable n n = 36 #Assign 36 to the variable n n = 310 #Assign 310 to the variable n xyz = 16.5 #Assign 16.5 to the variable xyz xyz = 265 #Assign 265 to the variable xyz xyz = "Free Learning Points" #Assign a string to the variable xyz
Note that once the value of a variable is modified, the old value is replaced with new value and old value cannot be recovered. In other words, a variable can hold only one value.
In addition to assigning a single value, you can also assign the result of the expression to a variable, for example:
sum = 230 + 45 #Assign the result of the addition to the variable mixes = 31 * 29% 6 #Assign the remainder to a variable str = "Welcome to " + "Free Learning Points" #Assign the result of string concatenation to a variable
When using Python variable, all you need to know is the name of the variable. Variables can be used almost anywhere in Python code, you can refer to the following example:
n = 12 print (n) #pass variable to function 12 m = n * 11 + 5 # treat variables as part of four arithmetic operations print (m) print (m-2) #Pass an expression consisting of variables as a parameter to the function m = m * 2 #Double the value of the variable itself print (m) website = "Free Learning Points" str = "Python tutorial:" + "website" #string concatenation print (str)
The output is:
12
137
135
274
Python tutorial: website
Python has two characteristics:
a. Variable can be assigned directly without declaration. Assigning a value to a non-existent variable is same as defining a new variable.
b. The data type of a variable can be changed at any time. For example:
value= 1234 (1234 is integer type)
string = “Welcome to Free Learning Platform” (Welcome to Free Learning Platform is string type)
When we use the print () function, we only produce one variable, but in fact, the print () function can produce multiple variables at the same time, and it has more rich functions.
The detailed syntax of the print () function is as follows:
print (value,..., sep='', end='\n', file=sys.stdout, flush=False)
As can be seen from the syntax format above, the value parameter can accept any number of variables or values, so the print () function can output multiple values. For example, the following code:
name = "Alice" age = 18 #Output multiple variables and strings at the same time print("User name:", name, "Age:", age)
The output is:
User name: Alice Age:18
From the output results, when using the print () function to produce multiple variables, the print () function separates multiple variables with spaces by default. If you want to change the default separator, you can set it with the sep parameter. For example, the output statement:
#Output multiple variables and strings at the same time, specify the separator print ("User name:", name, "Age:", age, sep = '|')
The output is:
User name: |Alice| Age:|18
In default case, the print() function always generate new line. This is because the default value of the end parameter of the print() function is “\n”. This “\n” represents a newline. If you do not want to generate new line, you can reset the end parameter, for example:
print(1) print(2) print(3)
The output is:
1
2
3
#Set the end parameter (use anaconda) print (4, end = "") print (5, end = "") print (6, end = "")
The output is:
4 5 6
The three print () statements above will execute three times of output, but since they all specify end = "", the output of each print () statement will remain on the same line.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises