Python Basics
Python code changes to bytecode to machine code last execution
The ' file name. PYc ' file that appears when the ' file name. py ' file is executed as a byte-code caching mechanism
When using Pycharm, add the following two lines of code at the beginning of the file, with the Chinese part explaining
#!/user/bin/env python with execute the following command
#-*-coding:utf-8-*-uses a code of UTF-8
In order not to need to add these two lines of code every time you write a file, we can set the base code of the py file in Pycharm, that is, the code that will be generated when the py file is created
Link
Import lib# Find lib.py, replace the file contents with the import Lib, where the lib.py for the program module here lib.py for their own established command module
"""
Where # can only comment on one line of content
This form can annotate multiple lines of content
With many modules in py
"""
Import modules using Import
ImportGetpass#Loading ModulesI1=raw_input ("Please enter user name:")#Raw_input for interacting with users, waiting for input content#Python2.7 uses Raw_input for interacting with users, waiting for input, using input in 3#i2=raw_input ("Please enter password:")I2=getpass.getpass ("Please enter your password")#The password is not displayed when you enter a password after using the loaded module command herePrint(I1)Print(I2)#in python2.x, you can follow the print without parentheses
Create a PY file process
1. Create a xxx.py file
PS: Do not have Chinese path
2. Write code
A. Two lines in the head
#!/user/bin/env python
#-*-Coding:utf-8-*-
B. Write function code
Write print with no spaces in the middle ("command")
3. Execute Code Code
A. Open the terminal
Function Key +r
B. Path to Python code files
Definition of variable names in Python
O= "xx is xx"
#o变量名
Can only be numbers, letters, underscores
Alex=123
sb= "Alex"
a_lex= "SB"
Cannot start with a number
1alex
Variable name cannot be a keyword inside python
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ' ~ ~ ~]
Basic data Types :
Numbers: 1231
Age=18
String:
a1= "ASDF"
a1= ' DASD '
A1= "" "AFASD" ""
Just enclose the string in double quotation marks.
Boolean value:
True/false
A4=ture opening caps
A5=false
one = for assignment two for comparison
If statement
If conditional statement notation
If condition:
code block
Elif Conditions:
code block
Else condition:
code block
The judgment of this if statement is that when the user input username is Alex and the password is 123, the output is yes, no output no
Name=raw_input ("Username") PWD=raw_input ("Password")ifname=="Alex" andpwd=="123":Print("Yes")Else:Print("No")
This string of code means that when the user enters a value of 2, Output 222, when the user input 3 output 333, otherwise output ..., note that the data format received from input is a string.
Inp=raw_input (">>>")ifFalse:Print("111")elifinp=="2":Print("222")elifinp=="3":Print("333")Else:Print("...")
While conditional statement
While condition:
code block #while loop similar C language condition is true always loops
Import timen1=true while N1: #a while loop that has the same C language condition as true ends the loop print( "1") Time.sleep (1) # This command is a pause for one second n1=False Print("end")
The following code ends the Wheli statement when Kaishi is 10.
Import Timekaishi=1 while kaishi<=10: print(kaishi) Kaishi=kaishi+1 Time.sleep (1)print("end")
Python basics, importing modules, if statements, while statements