Day1 - Introduction to Python foundation 1, basic syntax, flow control
1. Introduction to Python
Note: Programming languages are mainly classified from these perspectives, compiled vs interpreted, dynamic vs static languages, and strongly defined vs weakly defined languages
1.1. Python is a dynamically interpreted strongly typed language
Dynamic type language: refers to the language that checks the data type when the program is executed. The data type is not specified during programming, and the first time the program runs
When assigning a value to a variable, the data type is recorded inside the program
Statically typed languages: check data types at compile time and declare the data types of all variables at programming time
Interpretation type: Only when the program is executed, it is interpreted into machine language one by one for execution, which has low operating efficiency, such as Python/Ruby
Compiler: Compile each statement of the source program into machine language and save it as a binary file, which runs very fast, such as C/C++
Strongly Typed Language: Refers to a language that enforces the definition of data types, unless the variable data type is coerced, the data type will never be
Variable, is a type-safe language
Weakly typed language: a variable can be assigned values of different data types
1.2. Advantages and disadvantages of Python
Advantages: 1. High-level language, simple and easy to learn; 2. The code is highly portable and extensible, and supports multiple platforms; 3. The development efficiency of rich third-party libraries is high
Disadvantages: 1. Slow running speed; 2. Code cannot be encrypted; 3. Does not support multi-threading
1.3. Interpreter
The installed python comes with the official interpreter CPython, which is the most widely used interpreter
.pyc is the file generated when python is compiled
2. Basic grammar
2.1. Variables
constitute:
-
- Variable names can only be a combination of letters, numbers and underscores
- Cannot use special characters, cannot start with a number
- Keywords cannot be declared as variable names
Notation:
-
- The "my_name" variable name should be meaningful and use underscores, which is recommended by Python .
- "MyName" camel case, C/C++, etc. generally use this method
2.2. Character encoding and binary
Character encoding: ASCII (early, only supports English) ---> GB2312 (supports 7K Chinese characters) ---> GBK18030 (supports 20K multi Chinese characters) ---> GBK (supports 70K multi Chinese characters) ---> Unicode ( Universal code, supports all national characters)
UTF-8: Variable-length version of unicode, which can save space, and characters occupy space between 1 and 4 bytes
2.3. User Interaction Mode
Python2.X:raw_input("Please input:")----->Python3.X:input("Please input:")
3.for loop
Iterate over the elements of all sequences, including lists, strings, etc.
# -*- coding:utf-8 -*- import sys # 1. Traverse the string for letter in ' Hello ' : print ( " letter: " ,letter) # 2. Traverse list elements by index fruits = [ ' apple ' , ' banana ' , ' mango ' ] for index in range(len(fruits)): print ( " Current fruit: " ,fruits[index]) # 3.for....else statement # The else statement will be executed after the normal execution of the for is completed, that is, the for is not an interrupted for i in range(3 ): print (i) else : print ( "The for loop is normal end display " ) for i in range(3 ): print (i) break # for loop ends abnormally else : print ( " for loop ends normally and displays " ) # will not be executed
4.while loop
Used to execute programs in a loop, repeating the same task when a certain condition is met
#-*- coding:utf-8 -*- #Simple statement group flag = 1 while(flag):print('Good!') #Normal loop count = 0 while (count < 10 ): print ( "The current value is: " ,count) count +=1 print ( " >10 loop ends " ) # infinite loop var = 1 while (var == 1): num = input( " Haha, you can't get out if you come in: " ) print ( " What you input is: " ,num) # while....else loop #Execute when the while loop statement is false count = 1 while count < 5 : print ( " %s is less than 5, continue " % count) count +=1 else : print ( " More than 5, can't continue, Gameover! " )
5.break and contiune
break: jump out of the loop continue: jump out of this loop
# contiune: jump out of this loop for i in range(10 ): i +=1 if i == 5 : continue #print all except 5 print (i) count = 0 while (count <10): count +=1 if count == 5 : continue #Print (count) except 5 # break: Jump out of this loop for i in range(10 ): i +=1 if i == 5 : break # End after 5 without printing print (i) count = 0 while (count <10): count +=1 if count == 5 : break # End after 5 without printing print (count)
6. Table over type if..elif..else statement
Else: Executed when the if statement is 0 or false, it is optional, but an if can only correspond to one else
elif: allows to check whether multiple results are true, and execute the corresponding code block when true, elif is also optional, but can be any number