1, write a Python script under Linux, the beginning of the interpreter declaration should be #!/usr/bin/env python
Define variable name= "Liubo" age=21 all quotes are strings
The meaning of the variable is to indicate the data, so the variable name should make sense
Complex variable names are encountered using "_" to split multiple words or capitalize the first letter of a word
Son_of_twins_brother_age = 2
NAMEOFTWINSGF = "Fengjie"
You cannot use "-" to connect words
You cannot start with a number
You cannot use special characters
There can be no spaces in variable names
You cannot use a keyword as a variable
In Python2. X is supported by default ASCII encoding, if required in Chinese need to explicitly indicate the encoding table, the wording is:
#! /usr/bin/env python
#_ *_ Coding:utf-8 _*_
Python3 uses UTF-8 encoding by default, so no special announcements are required, only
#! /usr/bin/env Python is used to declare an interpreter (Linux runs)
Python development specification, maximum of 80 characters per line
Comments
#用于单行注释
Use 3 quotation marks (three each before and after) for multiple lines of comment
‘‘‘
Xxx
Xxx
‘‘‘
Print user-entered content
The notation in 3.0
User_input = input ("Input your Name:")
Print ("User input msg:", User_input)
The notation in 2.7
User_input = raw_input ("Input your Name:")
Print ("User input msg:", User_input)
In 2.7, input will assume that you are not entering a string, but a variable name, so if you want to pay the input to the variable, you should use Raw_input. Otherwise it will be an error
But if input is a number, it will be considered a number. So in 2.7, input can be a number, or it can be a variable name, and if it is a string, only raw_input
Therefore, the 2.7 basic does not use input
formatting strings
#!/usr/bin/envpython
#_ *_coding:utf-8_*_
Name=input ("Inputyourname:")
Age=input ("Inputyourage:")
Job=input ("Inputyourjob:")
Msg= ""
informationforuser:%s
-----------------------
name:%s
age:%s
job:%s
-----------END------------
"% (name,name,age,job) #% has spaces before and after
Print (msg)
%s represents a string and can be substituted with%d for the number entered
However, if you want to use%d, you must make sure that the variable is a numeric type and that input is a string type, so the age variable cannot use%d directly, and the input is converted to the INT type
That
Age=int (Input ("Input your Age:"))
Name=input ("Inputyourname:")
Age=int (Input ("Inputyourage:"))
Job=input ("Inputyourjob:")
Msg= ""
informationforuser:%s
-----------------------
name:%s
age:%d
job:%s
-----------END------------
"% (name,name,age,job) There are spaces before and after #%
Print (msg)
The formatted data type has 3
$s string
$d numbers
$f Decimals
Import module (the imported content can only be used under Windows CMD or Linux, Pycharm is not available)
Import Getpass #导入一个可以使输入变成密文的模块
Username = input ("Username:")
Password = getpass.getpass ("Password:")
Import OS #导入系统命令模块
Os.system ("DF") #用于打印系统命令
Os.mkdir ("/root/123") #用于创建一个文件夹
Os.system ("df-h") the output of this command is 2, one is the actual command output, one is the output, the command output is the content of DF (System storage usage), the output is 0
If we direct cmd = Os.system ("Df-h"), then only the output in cmd, if you want to copy the command output should be written as
cmd = Os.popen ("Df-h"). Read (), first save the results in memory with Popen, and then read
The module that follows the actual import command is a Python script, but cannot take a. py suffix
However, the location of this script needs to be noted, by default the system looks for a script under a specific path, or under the current path. The method for finding a specific path is
Import Sys
Print (Sys.path)
>>> Import Sys
>>> Print (Sys.path)
[', '/usr/local/python3/lib/python35.zip ', '/usr/local/python3/lib/python3.5 ', '/usr/local/python3/lib/python3.5 /plat-linux ', '/usr/local/python3/lib/python3.5/lib-dynload ', '/usr/local/python3/lib/python3.5/site-packages ']
You can see from here that the first seat is ', so the current path is the highest priority, so if there is a script with the same name in another path under the current path, the current path takes precedence
We can write a module on our own, using the previous
Import Getpass
Username = input ("Username:")
Password = getpass.getpass ("Password:")
Print (Username,password)
Save it as pass.py, then save it to the path above, and then import the pass in the other script to get the module imported.
If judgment
Python is a language with strong formatting and its syntax is formatted according to strict indentation
User= "Liubo"
Passwd= "Cisco,123"
Username=input ("inputyourusername;")
Password=input ("Inputyourpassword:")
If user = = Username:
Print ("Usernameiscorrect ...")
if passwd = = password:
Print ("Welcomelogin ...")
Else
Print ("Passwordisinvalid ...")
Else
Print ("Usernameisinvalid")
At the same time, if can be judged by multiple conditions
User= "Liubo"
Passwd= "Cisco,123"
Username=input ("inputyourusername;")
Password=input ("Inputyourpassword:")
If User==username and Passwd==password:
Print ("Welcome login")
Else
Print ("Usernameorpassworisinvalid")
Cycle
For I in range: Range (10) represents 10 numbers from 0 to 9
Loop 10 times, but guess 3 times, more than 3 times automatically jump out
Age=32
Foriinrange (10):
Ifi<3:
Gage=int (Input ("Inputage:"))
Ifgage>age:
Print ("Guessmorethanage")
Elifgage<age:
Print ("Guesslessthanage")
Else
Print ("Yougotit")
Break
Else
Print ("Toomanytimes")
Break
For I in range (10)
i=100
In the above content, range (10) represents a sequence, so in the for loop each time I is the value of the sequence is assigned value, even if the following re-assignment, as long as it is still in this loop, the I will still be covered by the range
Jump out of programs
1, jump out of the entire program, break, set to complete, when running to break, will end the program
2, jump out when the cycle, continue, set to complete, when running to continue, will end when the second cycle, but the overall cycle will still have
Job One: Blog
Job Two: Writing a login program
Enter User name password
Show welcome message after successful authentication
Three-time error after locking
Job Three: Multilevel menu
1. Beijing
2. Shanghai
>>input
1. Dongcheng
2. Xicheng
>>input
1. Shahe
>>input
Enter b at the same time to return to the parent menu, Q exit Program
Flowchart processon.com
Readme:
Svn:
Job Naming Day1
Python First day course