Article directory
Variables and Data Types
1. Definition of variables
For data that is reused and often needs to be modified, it can be defined as a variable to improve programming efficiency.
The syntax for defining a variable is: variable name = variable value. (The role of = here is assignment.)
After defining a variable, you can use the variable name to access the variable value.
For example:
# 不使用变量打印三次 "今天天气真好",如果需要变成打印 "今天天气不好" 需要修改三行代码
print("今天天气真好")
print("今天天气真好")
print("今天天气真好")
# 定义一个变量表示这个字符串。如果需要修改内容,只需要修改变量对应的值即可
weather = "今天天气真好"
print(weather) # 注意,变量名不需要使用引号包裹
print(weather)
print(weather)
illustrate:
- The so-called variable name can be understood as a box. When a variable value needs to be used, the corresponding box is brought.
- A variable is a variable quantity that can be modified at any time.
- Programs are used to process data, and variables are used to store data.
Two, the type of variable
In the program: In order to meet different business needs in Python, the data is also divided into different types. As shown below:
Third, view the data type
-
In python, as long as a variable is defined and it has data, its type has been determined. We developers do not need to take the initiative to explain its type, the system will automatically identify it. That is to say, when using it, "variables have no type, but data have type".
-
For example, in the example below, we cannot predict the type of variable b, but the type of a can be confirmed based on the data.
-
If you want to check the data type stored in a variable temporarily, you can use
type(变量的名字)
to check the data type stored in the variable.
Identifiers and keywords
identifier
In computer programming languages, identifiers are the names used by users when programming. They are used to name variables, constants, functions, statement blocks, etc., so as to establish the relationship between names and usage.
Naming rules
- Identifiers consist of letters, underscores, and numbers, and numbers cannot begin with a number.
- Strictly case sensitive.
- Keywords cannot be used.
Thinking: Which of the following identifiers are correct and which are incorrect?
- fromNo12 (correct)
- from#12 (error, cannot use special characters)
- my_Boolean (true)
- my-Boolean (wrong, cannot use hyphens)
- Obj2 (correct)
- 2ndObj (error, cannot start with a number)
- myInt (correct)
- test1 (correct)
- Mike2jack (Correct)
- My_tExt (correct)
- _test (correct)
- test!32 (error, cannot use special characters)
- haha(da)tt (error, cannot use special characters)
- int (error, is keyword)
- jack_rose (correct)
- jack&rose (error, cannot use special characters)
- GUI (correct)
- GUI (error, cannot use special characters)
Naming conventions
- Identifier naming should be as the name implies.
- Give a meaningful name, and try to know what it means at a glance (to improve the readability of the code) For example: the name is defined as name, and the student is defined as student
a = "zhangsan" # bad
name = "zhangsan" # good
b = 23 # bad
age = 23 # good
- Follow certain naming conventions.
- The hump nomenclature is further divided into the big hump nomenclature and the small hump nomenclature.
- Lower camel case: The first word begins with a lowercase letter; the second word begins with a capital letter, for example: myName, aDog
- Upper camel case: The first letter of each word is capitalized, for example: FirstName, LastName.
- The hump nomenclature is further divided into the big hump nomenclature and the small hump nomenclature.
- Another nomenclature is to use an underscore "_" to connect all words, such as send_buf. Python's command rules follow the PEP8 standard:
- Variable names, function names and file names are all lowercase and connected with underscores;
- The class name follows the big hump naming method;
- Constant names are all uppercase;
keywords
The concept of keywords Some identifiers have special functions, which are called keywords. Keywords have been officially used by python, so developers are not allowed to define identifiers with the same name as keywords.
Keyword List:
False
, None
, True
, and
, , , as
, assert
, break
, , class
, continue
, def
, , del
, elif
, , else
, except
, finally
, for
, from
, global
, if
, import
, , , , , , , ,in
is
lambda
nonlocal
not
or
pass
raise
return
try
while
with
yield
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model