Home Backend Development Python Tutorial Summary of essential knowledge for learning Python

Summary of essential knowledge for learning Python

Jun 04, 2018 pm 05:40 PM
python Summary Knowledge

This article mainly introduces the relevant information that summarizes the necessary knowledge for learning Python. Friends who need it can refer to it

1. Variables
1. Variables
•refers to the variables during program execution. , a variable quantity;
• Defining a variable will be accompanied by three characteristics, namely memory ID, data type and variable value.
•Before running other languages, you must manually release the memory space of the program. However, the python interpreter has its own memory recycling mechanism. Once the python program finishes running, the memory space will be automatically released.

age=10
print(id(age),type(age),age)

2. Constant
•Refers to an immutable quantity during program execution ;
•Constants are generally defined in uppercase letters.

AGE=10
print(AGE)

3. Naming method of variables
•Camel case

AgeOfOldboy=72

• Underscore

age_of_oldboy=72

2. Interacting with the program
In ancient times, when we went to the bank to withdraw money, we needed a bank clerk waiting for us to enter our account and password to him. , and then he goes to verify it and after it is successful, we enter the withdrawal amount and tell him.
Proud modern people will provide customers with an ATM machine (that is, a computer), allowing the ATM machine to interact with the user, thus replacing manpower. However, the machine is dead, and we must write programs for it to run. This requires our programming language to have a mechanism that can interact with the user and receive user input data.

1.python3
•Python3 supports UTF-8 Chinese encoding by default. python2 needs to add # -*- coding:utf-8 -*- at the head of the code.
•Input in python3, no matter what type of value is entered, it will be saved as str (string) type

name=input('please enter the username: ')
print(id(name),type(name),name)
Copy after login
Copy after login

2.python2
•raw_input in python2 is the same as input in python3;

name=raw_input('please enter the username: ')
print(id(name),type(name),name)
Copy after login

•Input in python2, a value must be entered, and the value will be saved as what type it is.

name=input('please enter the username: ')
print(id(name),type(name),name)
Copy after login
Copy after login

3. Data type
1.int integer
•Generally used to define age, ID number, QQ number, grade, etc.

age=18
id=130530198805240011
qq=379048558
level=99
Copy after login

2.float floating point Type
•Generally used to define height, weight, salary, etc.

height=1.81
height=float(1.81)
Copy after login

3.str String type
•Generally used to define a person’s name, gender, status, etc.;
•General Strings are placed in single quotes, double quotes, or triple quotes.

name='egon'
sex='female'
age=18
Copy after login
Copy after login

•String splicing uses " "

name='egon'
sex='female'
age=18
Copy after login
Copy after login

print(name sex str(age))
Note: The age variable value here is 18, which is an integer and cannot be used as a character. For string concatenation, str(age) needs to be used to convert to string type.

•Use "*" for string splicing

name='egon'
print(name*10)
Copy after login

4.bool Boolean value type
•Only two values: True and False;
•Mostly used for judgment .

age=73
AGE=18
print(age < AGE)
print(age > AGE)
Copy after login

5. Conversion between various types
•Integer type——>Floating point type

a=18
print(float(a))

•Floating point type——>Integer type

a=1.81
print(int(a))

•Floating point type——>String type

a=1.81
print(str(a))

•Integer type——>String type

a=18
print(str(a) )

4. Array type
1. List []
•List in python is defined in [], and the elements are separated by "comma";

info=[&#39;egon&#39;,&#39;alex&#39;,18]
print(info[2])
Copy after login

•Elements can be any data type or array type;
•Character elements need to be enclosed in quotes, integers, floating point types, lists, etc. do not need quotes.

info=[13,18.1,&#39;alex&#39;,[&#39;egon&#39;,&#39;tony&#39;]]
print(info[3][0])
Copy after login

2. Dictionary {}
•Dictionary in python, also called associative array, is defined in {}, and the elements inside are expressed in the project name: project content format, and "comma" is used between elements "separated;

info={&#39;name&#39;:&#39;egon&#39;,&#39;sex&#39;:&#39;male&#39;,3:18}
print(info[3])
Copy after login

•The project content can be any data type, any array type;
•The string type in the project content needs to be enclosed in quotes, integer, floating point, Lists, etc. do not require quotes.

info={&#39;姓名&#39;:&#39;爱根&#39;,&#39;性别&#39;:&#39;男&#39;,&#39;肌肉&#39;:[&#39;有&#39;,&#39;无&#39;]}
print(info[&#39;肌肉&#39;][1])
 
info={&#39;姓名&#39;:&#39;爱根&#39;,&#39;性别&#39;:&#39;男&#39;,&#39;肌肉&#39;:123}
print(info[&#39;肌肉&#39;])
 
info={&#39;姓名&#39;:&#39;爱根&#39;,&#39;性别&#39;:&#39;男&#39;,&#39;肌肉&#39;:18.1}
print(info[&#39;肌肉&#39;])
 
info={&#39;姓名&#39;:&#39;爱根&#39;,&#39;性别&#39;:&#39;男&#39;,&#39;肌肉&#39;:&#39;无&#39;}
print(info[&#39;肌肉&#39;][1])
Copy after login

5. Formatted output
•my name is xxx, my age is xxx
•You need to use the placeholder %s

name=input(&#39;user_name>>: &#39;)
age=input(&#39;user_age>>: &#39;)
print(&#39;my name is %s, my age is %s&#39; %(name,age))
Copy after login

6. Operators
1. Arithmetic operators
• - * /

print(5+5) #5加5等于10
print(5-5) #5减5等于0
print(5*5) #5乘5等于25
print(5/2) #5除以2等于2.5
Copy after login

• Find the integer part of the quotient // Find the remainder part of the quotient % power **

print(5//2) #5除以2商等于2余1,只取商2
print(5%2) #5除以2商等于2余1,只取余数1
print(3**2) #3的2次幂是3乘3等于9
Copy after login

2. Comparison operators
• > < > •Logical AND, all conditions must be met before the result is True;

•Logical or or, only one condition is met, the result is True;

•Logical NOT, the result is negated.

print(30 > 20)
print(30 < 20)
print(30 >= 30)
print(30 <= 30)
print(30 == 30)
print(30 != 40)
Copy after login



The above is the detailed content of Summary of essential knowledge for learning Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

See all articles