4. Basic use of variables
4.1 Variable Definition
- In Python, each variable must be assigned a value before it is used, and the variable will not be created until the variable is assigned
- The equal sign (=) is used to assign values to variables
- = on the left is the variable name
- = on the right is the value stored in the variable
variable name = value
After the variable is defined, it can be used directly later
4.2 Types of variables
- Create a variable in memory that will contain:
Name, stored data, type of stored data, address (label)
- Defining variables in Python does not require specifying a type (as is required in many other high-level languages)
- Data types can be divided into numeric and non- numeric
- digital
- Integer (int)
- floating point (float)
- boolean (bool)
- True True non-zero number - non-zero is true
- false False 0
- complex
- Mainly used for scientific calculations, such as: plane field problems, wave problems, inductance and capacitance problems, etc.
- non-numeric
- string
- list
- tuple
- dictionary
Hint: In Python 2.x, integers are further classified according to the length of the stored value:
-
- int (integer)
- long (long integer)
- Use the type function to see the type of a variable
In [1]: type(name)
4.3.1 Computation between variables of different types
1) Numerical variables can be directly calculated
- In Python, two numeric variables can perform arithmetic operations directly
- If the variable is of type bool, when calculating
- True corresponds to the number 1
- False corresponds to 0
2) Use + to concatenate strings between string variables
- In Python, you can use + concatenation between strings to generate new strings
In [1]: first_name = "三" In [2]: last_name = "张" In [3]: first_name + last_name Out[3]: 'Three cards'
3) String variables can use * to repeatedly concatenate the same string with integers
In [1]: "-" * 50 Out[1]: '--------------------------------------------------'
4) No other calculations can be performed between numeric variables and strings
In [1]: first_name = "zhang" In [2]: x = 10 In [3]: x + first_name --------------------------------------------------------------------------- TypeError: unsupported operand type(s) for +: 'int' and 'str' TypeError: `+` Unsupported operation types: `int` and `str`
4.3.2 Variable Input
- The so-called input is to use code to obtain the information entered by the user through the keyboard
- In Python, if you want to get the user's input information on the keyboard , you need to use the input function
function |
illustrate |
print(x) |
output x to the console |
type(x) |
View the variable type of x |
- In Python you can use the input function to wait for user input from the keyboard
- Anything entered by the user is considered a string
String variable = input("Prompt information:")
type conversion function
function |
illustrate |
int(x) |
convert x to an integer |
float(x) |
convert x to a float |
Define a floating-point variable and use the float function to convert it while receiving user input
price = float(input("Please enter the price:"))
2.5 Formatted output of variables
The unit price of apple is 9.00 yuan/catties, if you buy 5.00 catties, you need to pay 45.00 yuan
- Use the print function to output formatted content
- % is known as the formatting operator and is used to handle string formatting
- A string containing % is called a format string
- % is used with different characters , different types of data need to use different formatting characters
formatting characters |
meaning |
%s |
string |
%d |
Signed decimal integer, %06d indicates the number of display digits of the output integer, and 0 is used to fill in the shortage. |
%f |
Floating point number, %.2f means that only two digits are displayed after the decimal point |
%% |
output % |
print("Format string" % variable 1) print("Format string" % (variable1, variable2...)) print("My name is %s, please take care of me!" % name) print("My student number is %06d" % student_no) print("The unit price of apples is %.02f yuan/catties, if you buy %.02f jins, you need to pay %.02f yuan" % (price, weight, money)) print("The data scale is %.02f%%" % (scale * 100))
Exercise - personal business card
need
- In the console, the user is prompted to enter: name , company , position , phone number , email address
- Output in the following format:
************************************************** company name Name (Position) phone: phone Email: Email **************************************************
Code:
""" In the console, prompt the user to input: name, company, position, phone number, email address """ name = input("Please enter your name: ") company = input("Please enter the company: ") title = input("Please enter the title: ") phone = input("请输入电话:") email = input("请输入邮箱:") print("*" * 50) print(company) print() print("%s (%s)" % (name, title)) print() print("电话:%s" % phone) print("邮箱:%s" % email) print("*" * 50)
5. 变量的命名
5.1 标识符
标示符就是程序员定义的 变量名、函数名
- 标示符可以由 字母、下划线 和 数字 组成
- 不能以数字开头
- 不能与关键字重名
- 关键字 就是在 Python 内部已经使用的标识符
- 关键字 具有特殊的功能和含义
- 开发者 不允许定义和关键字相同的名字的标示符
5.2 关键字
通过以下命令可以查看 Python 中的关键字
In [1]: import keyword In [2]: print(keyword.kwlist)
- import 关键字 可以导入一个 “工具包”
- 在 Python 中不同的工具包,提供有不同的工具
5.3 变量的命名规则
命名规则 可以被视为一种 惯例,并无绝对与强制 目的是为了 增加代码的识别和可读性
注意 Python 中的 标识符 是 区分大小写的
- 在定义变量时,= 的左右应该各保留一个空格
- 在 Python 中,可以按照以下方式命名
- 每个单词都使用小写字母
- 单词与单词之间使用 _下划线 连接
例如:first_name、last_name、qq_number、qq_password
驼峰命名法
- 小驼峰式命名法
第一个单词以小写字母开始,后续单词的首字母大写。例如:firstName、lastName
- 大驼峰式命名法
每一个单词的首字母都采用大写字母。例如:FirstName、LastName、CamelCase