Day1-python Basics 1 Introduction, basic syntax, Process Control
1.Python Introduction
Note: Programming languages are mainly categorized from these angles, compiled vs explanatory, dynamic vs static, and strongly defined vs weakly defined type languages
1.1.Python is a dynamically explanatory strong-type definition language
Dynamic type language: a language that checks data types when executing a program, does not specify a data type when programmed, and the first time the program runs
When assigning a value to a variable, the data type is recorded inside the program
Static type language: Check the data type at compile time, and declare the data type of all variables when programming
Explanatory type: Only when the program is executed, one article is interpreted into machine language execution, operating efficiency is low, such as Python/ruby
Compiled: Each statement of the source program is compiled into a machine language, and saved as a binary file, run quickly, such as C + +
Strongly typed definition language: Refers to a language that enforces the definition of a data type, unless the variable data type is coerced, the data type never
Change, is a type-safe language
Weak type definition language: A variable can assign values of different data types
Advantages and disadvantages of 1.2.Python
Advantages: 1. High-level language, simple and easy to learn; 2. Code portability is strong, support multi-platform; 3. Rich third-party library development efficiency
Cons: 1. Slow operation, 2. code cannot be encrypted; 3. Multithreading is not supported
1.3. Interpreter
The installed Python comes with the official interpreter CPython, which is the most widely used interpreter
. PYC is a file that is generated when Python executes
2. Basic syntax
2.1. Variables
Constitute:
-
- Variable names can only be combinations of letters, numbers, and underscores
- Cannot use special characters and cannot start with a number
- Keyword cannot be declared as a variable name
How to write:
-
- the name of the "my_name" variable is meaningful, and it is recommended for Python to use an underscore connection.
- "MyName" hump type, C + + and other general use this way
2.2. Character encoding and Binary
Character encoding: ASCII (early, English only)--->gb2312 (supports 7K multi-kanji)--->gbk18030 (supports 20K multi-kanji)--->GBK (supports 70K multi-kanji)--->unicode (Universal code, Support for all national characters)
UTF-8: Variable long version Unicode, space-saving, character-occupying space between 1~4 bytes
2.3. User interaction Mode
Python2.x:raw_input ("Please enter:")----->python3.x:input ("Please Enter:")
3.for Cycle
Iterate through all the elements of a sequence, including lists, strings, etc.
#-*-coding:utf-8-*-ImportSYS#1. Traversing a string forLetterinch 'Hello': Print("Letter :", letter)#2. Iterating through the list elements through an indexFruits = ['Apple','Banana','Mango'] forIndexinchrange (len (fruits)):Print("Current Fruit:", Fruits[index])#3.for....else Statements#The Else statement executes after the for normal execution is complete, that is, the for is not broken out by break forIinchRange (3): Print(i)Else: Print("For loop normal end display") forIinchRange (3): Print(i) Break #For loop Abnormal endElse: Print("For loop normal end display")#will not be executed
4.while Cycle
Used to loop the execution of a program, repeating the same task when a certain condition is met
#-*-coding:utf-8-*-#Simple Statement GroupFlag = 1 while(flag):Print('good!')#Normal CycleCount =0 while(Count < 10): Print("the current values are:", Count) Count+=1Print(">10 Cycle End")#Infinite Loopsvar = 1 while(var = = 1): Num= Input ("haha, come in will not go out:") Print("What you have entered is:", num)#While....else Cycle#executes when a while loop statement is FalseCount = 1 whileCount < 5: Print("%s is less than 5, continue"%count) Count+=1Else: Print("More than 5, can't go on, gameover! .")
5.break and Contiune
Break: Jump out of the loop Contiune: Jump out of the loop
#Contiune: Jump out of this cycle forIinchRange (10): I+=1ifi = = 5: Continue #Print all except 5 Print(i) Count=0 while(Count <10): Count+=1ifCount = = 5: Continue #Print all except 5 Print(count)#Break : Jump out of this cycle forIinchRange (10): I+=1ifi = = 5: Break #5 End after not printing Print(i) Count=0 while(Count <10): Count+=1ifCount = = 5: Break #5 End after not printing Print(count)
6. Table-Over if: Elif. Else statement
else: executed when the IF statement is 0 or false is optional, but an if can only correspond to an else
Elif: Allows you to check if multiple results are true, and when True, the corresponding code block is executed, elif is optional, but can be any
Day1-python Basics 1 Introduction, basic syntax, Process Control