Can variable types be declared in python

Variable (variable) is a very important concept in Python language. The main function of variables is to give a name to a value in a Python program. Names similar to "Zhang San", "Li Si" and "Wang Er Mazi" are easy to remember.

In the Python language, when you declare a variable, you need to assign a value to it. After all, variables that do not represent any value are meaningless, and such variables are not allowed in the Python language.

Declaring a variable is also very simple, the syntax structure is as follows:

variable_name = variable_value

The left side of the equal sign (=) is the variable name, and the right side is the variable value. After the value is assigned, the Python compiler will automatically identify the type of the variable.

Note: Variables cannot be named arbitrarily, they must conform to certain rules. Variable names usually contain letters, numbers and underscores (_), and variable names cannot start with a number. For example, value315 is a valid variable name, and 315value is an incorrect variable name.

The following code declares multiple variables, the data types of these variables include integer, string, Boolean, and floating point numbers. Finally output the values of these variables.

x =20           #Declare variables of integer type
y =40           #Declare variables of integer type
s ="I love python"   #Declare string type variable
flag = True         #Declare a Boolean variable
u =30.4          #Declare floating-point variables
print(flag)         #Output the value of the flag variable
print(x + y)        #Output the sum of x and y
print(s)          #Output the value of the s variable
print(u)          #Output the value of the u variable

Knowledge point expansion:

python declaration variable

Name and use of variables

When using variables in Python, you need to follow some rules and guidelines.
Violating these rules will cause errors, and the guidelines are designed to make the code you write easier to read and understand. Be sure to keep in mind the following rules regarding variables.
Variable names can only contain letters, numbers and underscores. Variable names can start with letters or underscores, but not numbers. For example, you can name the variable as

message_1, but it cannot be named 1_message.

Variable names cannot contain spaces, but underscores can be used to separate words. For example, the variable name greeting_message works, but the variable name greeting message will cause an error.

Do not use Python keywords and function names as variable names, that is, do not use words reserved by Python for special purposes, such as print.

Variable names should be short and descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.

Use the lowercase letter l and uppercase letter O with caution, because they may be mistaken for the numbers 1 and 0.

To create good variable names requires a certain amount of practice, especially when the program is complex and interesting. As you write more and more programs, and start to read the code written by others, you will become better at creating meaningful variable names.
Note that for now, lowercase Python variable names should be used. Although using capital letters in variable names will not cause errors, it is a good idea to avoid capital letters.

# Declare variable
message ="Hello Python world!"
 
# Print result
print(message)

The output of the code above is: Hello Python world!

The above is the details of whether variable types can be declared in python. For more information on whether variable types can be declared in python, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

Can variable types be declared in python
Is a number in python a variable type
Can python code be made into software
Functions in python
Python learning-variable types
Can the value of the python dictionary be modified?
Python iterators can also be played like this
Centos install security dog can be completed in 5 steps
What can Python do
Join function in Python
12. Network Programming in Python3
print statement in python
Python basic data types
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Python basic data types
Write gui in python
MongoDB usage in Python
Can python develop games
Str string in Python
Computational Geometry in Python
Interesting applications that can be used for teaching in ubuntu16.04