Learning content:
1, Python Introduction
2. Python program initial contact and variables
3. Python User interaction
4. Python data type
5. Python Loop If ... (elif) ... else
6. Python Loop While...else
7. Python Loop For...else
8. The Python loop interrupts break and continue
I. Python introduction 1) Python Pros and cons
First look at the advantages:
- Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
- Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
- High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
- Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
- Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
- Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.
Look at the disadvantages again:
- Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.1s, with Python is 0.01s, so c directly than Python faster 10s, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. In fact, in most cases python has been fully able to meet your requirements for the speed of the program, unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
- Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
- Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil. A compromise solution to this problem is discussed in more detail later in the Threads and Processes section.
Second, the Python program initial contact and variables
Print ("hello,world! ") #第一个python程序
b=2
C=3
Print (B,C) #变量赋值
B=22
Print (B,C) #变量指向内存地址
Name= "Spider"
Print (name) #变量赋值 (String)
Spider = "Spider" #python2与python3区别
Print (spider)
Third, Python user interaction1) input and Raw_input
Input in Python2 is the input in Python3 (consistent usage)
Raw_input in Python2, what type of input is typed, and no raw_input in Python3
2) Input usage and parameter transfer mode:
Name = input (' Name: ')
Age = Input (' Age: ')
Print (Type (age))
Job = input (' job: ')
Salary = input (' Salary: ')
Info3= ""
-----information of Info3 {0}
NAME:{0}
Age:{1}
JOB:{2}
SALARY={3}
". Format (name,age,job,salary)
Print (INFO3)
Info2= ""
------information of Info2 {_name}------
Name:{_name}
Age: {_age}
Job: {_job}
Salary:{_salary}
". Format (_name=name,
_age=age,
_job=job,
_salary=salary)
Print (Info2)
Info1 = "'
-------Information of info1%s-------
name:%s
Age:%s
Job:%s
Salary:%s
"% (name,name,age,job,salary)
Print (INFO1)
Info= ""
---------Information of info "+ name +"--------
Name: "+ name +"
Age: "+ Age +"
Job: ' + Job + '
Salary: "' + Salary
Print (info)
Iv. python data type and character encoding format 1) data type
numeric int floating-point F String S bool:ture and False
2) Character encoding
Ascii:american Standards Code for information Interchange, US standard Information Interchange code 1byte
gb2312:1980, supporting 7,000 Chinese, accounting for 2bytes
gbk:1995, supporting 20,000 Chinese, accounting for 2bytes
The birth of gb18030:2000, supporting the Mongolian, Tibetan, and other characters, supporting 27,000 text, accounting for 2bytes
Note: The above are backwards compatible;
Later Unicode was born, also called the Universal Code, unified code, support the above format, compatible with other languages, accounting for 2bytes
Due to waste of resources, UTF-8 birth, variable-length unicode,ascii content accounted for 1byte, European characters accounted for 2bytes, East Asian characters accounted for 3bytes (naked exploitation)
V. Python loop If ... (elif) ... else
#密文密码输入 getpass Module pycharm, can be implemented under Linux system
Import Getpass
"' #输入用户名和密码正确欢迎, error returned error message
_username = ' Spider '
_password = ' 123 '
Username = input ("Please enter user name:")
#password = Getpass.getpass ("Please enter password:") #密文输入
password= input ("Please enter password:")
If username = = _username and Password = = _password:
Print ("Welcome user {a} login ...". Format (A=username))
Else
Print ("Invalid username or password")
Print (Username,password)
‘‘‘
Six, Python loop while...else
Age_of_oldboy = #猜数字, printing information, up to three guesses, error return information
Count = 0
While count<3:
guess_age = Int (input ("Gusee Oldboy ' Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got It!")
Break
Elif guess_age > Age_of_oldboy:
Print ("You are wrong,guess smaller ...")
Else
Print ("You are wrong,guess bigger!")
Count +=1
Else
Print ("You have tried too many times. Please go out! ")
Seven, Python loop for...else
Age_of_oldboy = #猜数字, printing information, up to three guesses, error return information
For count in range (3):
guess_age = Int (input ("Gusee Oldboy ' Age:"))
if guess_age = = Age_of_oldboy:
Print ("Yes,you got It!")
Break
Elif guess_age > Age_of_oldboy:
Print ("You are wrong,guess smaller ...")
Else
Print ("You are wrong,guess bigger!")
Eight, the Python loop interrupts break and continue1) breaks out of the loop; Continue jumps out of the loop
Example 1:
For I in Range (0,10): #i <5 continues to execute downward, print j,i>5 does not go down, does not print J
Print ("---------", i)
If i> 5:
Continue
For j in Range (0,10): #j <5 Prints j,j>5 jumps out of the loop
Print (j)
If J > 5:
Break
Example 2:
For I in Range (0,10): #当i >5, jumps out of the loop, does not execute down, when i<5, executes down
Print ("---------", i)
If i> 5:
Break
For j in Range (0,10):
Print (j)
If J > 5:
Continue
Python Learning Path _python Basics (1)