Python basics 1: Python Basics
I. Introduction and version of Python
Python is an excellent and widely used language that advocates elegance, clarity, and simplicity.
Currently, Python is mainly used in the following fields:
Cloud computing:The most popular cloud computing Language
WEB development:Many excellent WEB frameworks, many large websites are developed in Python, and typical WEB frameworks include Django
Scientific Computing and artificial intelligence:Typical libraries: NumPy, SciPy, Matplotlib, Enthought librarys, and pandas
System O & M:Required language for O & M personnel
Finance:Quantitative transaction and Financial Analysis
Graphical GUI: PyQT, WxPython, TkInter
Ii. Classification and advantages and disadvantages of programming languages
Interpreted type: when the program runs, one line of code is interpreted as binary and one line is interpreted as running.
Advantages: Fast troubleshooting, high development efficiency, cross-platform
Disadvantage: relatively low program running performance
Typical: Python
Compile type: compile all program code into binary at a time and then run it again.
Advantage: High procedural performance
Disadvantages: Slow troubleshooting, high development efficiency, and cross-platform failure
Typical: C Language
3. Run the first python Program
#!/usr/bin/env python# -*- coding: utf-8 -*-print("hello,world!")
Iv. Variables 5. Constants
Unchanged quantity: Date of birth, ID card number, etc. Python does not stipulate that all uppercase variables will be Constants by default, and BIRTH = 19970425
6. Notes
Purpose: help you remember the role of the code at any time, and others can easily understand your code.
Type:
- Single line comment :#. Example: # commented content
- Multi-line comment: ''' commented content ''' or "commented content """
VII. Basic Data Types
Type () determines the Data type of the data.
1. Number Type int
Used for calculation or comparison.
i = 2print(i * 3)
2. string type str
Used as a string (enclosed in quotation marks is a string)
Name = 'old boys' name2 = 'lufei 'print (name,' and ', name2) #' mixed use with "prevent confusion msg =" My name is jim, i'm 26 years old! "Msg = ''' today, I want to write a small poem to praise my deskmate. You see his short black hair, like a fried chicken. ''' # String concatenation n1 = 'You can view the blog in 'n2 =' n3 = n1 + n2print (n3)
3. Boolean Value
Boolean value is used to determine whether the result is correct. The result can only be True or False.
print(1 > 2 and 3 < 4 or 4 > 5)
8. User Interaction
All input data types are strings.
Python 2x: raw_input ()
Python 3x: input ()
Name = input ("enter your name:") age = input ("Enter your age:") Hober = input ("Enter your hobby :") s = "Your name is" + name + ", this year" + age + "years old," + "hobby" + hobbyprint ("My name is % s, my age is % s, my hobby % s "% (name, age, holobby) print (s)
9. Some basic structures of if and while Loops
If
If condition: Result
If condition: Result else: Result
1 choice = input ('Enter the number you guessed: ') 2 if choice = '2': 3 print ('2') 4 elif choice = '3 ': 5 print ('3') 6 elif choice = '4': 7 print ('4 ')
1 choice = input ('Enter the number you guessed: ') 2 if choice = '2': 3 print ('2') 4 elif choice = '3 ': 5 print ('3') 6 elif choice = '4': 7 print ('4') 8 else: 9 print ('select error... ')
1 if condition: 2 if condition: 3 Result 4 else: 5 result 6 else: 7 result
1 age = int (input ('Guess my age: ') 2 if True: 3 if age <= print ('Congratulations! You guessed it! ') 5 else: 6 print ('All of them are shown...') 7 else: 8 print (666)
While
While condition: Result
While True: print ('qinghua') print ('Princess Wang ') print ('coolly') print ('late night Underground Iron') print (222) while True: print (111) print (222) print (444)
Conditions for jumping out of the loop:
1. Change Conditions
2. break
Break: End Loop
Continue: end this loop and continue the next loop
Flag = Truewhile flag: print ('wait a minute ') print ('your backpacker') print ('color black') flag = False
flag = Truecount = 1while flag:print(count)count = count + 1if count == 101:flag = False
while True:print(111)print(222)breakprint(333)
count = 1while True:print(count)count = count + 1if count == 101:break