[Learn python from zero] 04. Python programming basics: variables, data types and identifiers

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:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-oLIHpPIT-1691466461573) (picture link)]

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.
    insert image description here

  • 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

insert image description here

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.
      insert image description here
    • 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.
  • 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, , , , , , , ,inislambdanonlocalnotorpassraisereturntrywhilewithyield

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

[python] Use Selenium and Chrome WebDriver to obtain article information in [Tencent Cloud Studio Practical Training Camp]

Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition

[Fun with Python series [Xiaobai must see] Python multi-threaded crawler: download pictures of emoticon package websites

[Play with Python series] [Must-see for Xiaobai] Use Python to crawl historical data of Shuangseqiu and analyze it visually

[Play with python series] [Must-see for Xiaobai] Use Python crawler technology to obtain proxy IP and save it to a file

[Must-see for Xiaobai] Python image synthesis example using PIL library to realize the synthesis of multiple images by ranks and columns

[Xiaobai must see] Python crawler actual combat downloads pictures of goddesses in batches and saves them locally

[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

[Must-see for Xiaobai] Technical realization of using Python to download League of Legends skin pictures in batches

[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

[Must-see for Xiaobai] Python crawler combat: get pictures from Onmyoji website and save them automatically

Xiaobai must-see series of library management system - sample code for login and registration functions

100 Cases of Xiaobai's Actual Combat: A Complete and Simple Shuangseqiu Lottery Winning Judgment Program, Suitable for Xiaobai Getting Started

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

Introductory Guide to Crawlers (7): Using Selenium and BeautifulSoup to Crawl Douban Movie Top250 Example Explanation [Reptile Xiaobai must watch]

Getting Started Guide to Crawlers (6): Anti-crawlers and advanced skills: IP proxy, User-Agent disguise, Cookie bypass login verification and verification code identification tools

Introductory Guide to Crawlers (5): Distributed Crawlers and Concurrency Control [Implementation methods to improve crawling efficiency and request rationality control]

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

Application of Deep Learning Model in Image Recognition: CIFAR-10 Dataset Practice and Accuracy Analysis

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

Use Python and Selenium to automate crawling#【Dragon Boat Festival Special Call for Papers】Explore the ultimate technology, and the future will be due to you"Zong" #Contributed articles

Python multi-thread and multi-process tutorial: comprehensive analysis, code cases and optimization skills

Selenium Automation Toolset - Complete Guide and Tutorials

Python web crawler basics advanced to actual combat tutorial

Python introductory tutorial: master the basic knowledge of for loop, while loop, string operation, file reading and writing and exception handling

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

Introductory Python programming basics and advanced skills, web development, data analysis, and machine learning and artificial intelligence

Graph prediction results with 4 regression methods: Vector Regression, Random Forest Regression, Linear Regression, K-Nearest Neighbors Regression

Guess you like

Origin blog.csdn.net/qq_33681891/article/details/132163781