Python environment we will use more than 3.x version, because the official said python3.x above version is the future needs of the language, transition version 2.6, 2.7 will not be updated, In other words, there will be no 2.8 this version, if you want to continue to use Python, everyone will talk about the business move to more than 3.x. Because teaching so will be mixed, (in fact, I do not like mixing, who let others are teachers).
Okay, here we go:
1. First use Python to play Hello world!
Enter the Python editor
#python
>>> print ("Hello world!")
Hello world!
2. Writing Python script considerations
#!/usr/bin/env python #指定脚本的解释器类似shell #!/bin/bash Only then does the computer know what language you are writing scripts in.
#缩进
Because Python's control statements do not have a shell-like closing sentence, for example:
#shell for statement
For n in {1..5}
Do
Echo $n
Done
#python for statement
For n in {1..5}:
Echo $n
Python controls the end of a process through tight indentation
3. Variables
Can be repeated calls, no longer script to write a large number of code, convenient, efficient.
(1) Rules for variable definitions:
One, can only use any combination of letters, numbers and underscores
Second, the variable name cannot start with a number
Three, the following keywords cannot use the variable name
' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' fr ' Om ', ' global ', ' if ',
' Import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' with ', ' yield '
(2) Assigning values to variables
>>> name = "ZZN" #如果赋值是字符串要加引号
>>> Print Name
Zzn
>>> age = #如果赋值是数字则不需要加引号
>>> Print Age
21st
PS: we can see the ID of the assignment in memory by ID (variable name)
>>> ID (name), ID (age)
(39424776, 39424776)
4. User interaction
ps:python2.x's interactive command is Raw_input (), and python3.x's interactive command is input () we press 2.x, 3.x I think it's a bit remote, foreign companies basically have 3.x
(1) We write an interactive statement: What's your name?
>>> Raw_input ("What is You name:")
What is you NAME:ZZN
' Zzn '
Write the following script:
#vim test.py #!/usr/bin/env python name = Raw_input ("What is Name:") Print name #python test.py what is you NAME:ZZN Zzn
This is a small script for user interaction
5. Conditional statements
(1) If judgment
Example:
#vim test.py #!/usr/bin/env python name = Raw_input ("What is Your Name:") Age = Raw_input ("What Is Your Age:") if name = = "ZZN": Print Name,age
Else
Print ("Wo cuo le!")
#python test.py What is you name:zzn age:21 zzn 21
(2) For statement
Example:
#!/usr/bin/env python
sum = 10
for n in range(3):
sums = int(input("please you input sum:"))
if sums > sum:
print ("you sum da")
elif sums < sum:
print ("you sum xiao")
else:
print ("bingo")
break #跳出整个循环
(3) While statement
#!/usr/bin/env python
sum = 10
cai = 0
while cai < 3:
print ("cai",cai)
sums = int(input("please you input sum:"))
if sums > sum:
print ("you sum da")
elif sums < sum:
print ("you sum xiao")
#elif sums == 10:
else:
print ("bingo")
break
cai += 1
else:
print("To many option")
6. Data type
(1) Number
int (shaping) If you are a 32-bit system int's range of values is -2**31:2**31-1: -2147483648?2147483647 64-bit system is also
Long (longer shaping) theoretically the range of values is unlimited
Float (floating point type) is the decimal point
(2) Boolean value
True or False 1 or 0
(3) string
"Hello World"
I. String concatenation output
#vim test.py
#!/usr/bin/env python
Name = Raw_input ("What is You name:")
Age = Raw_input ("What Is Your Age:")
Sex = Raw_input ("What is You Sex:")
Job = Raw_input ("What Is Job:")
Print ("Ni yao cha kan:" + name + "\nname:" + name + "\nage:" + Age + "\nsex:" + Sex + "\njob:" + job + "\ n")
#python test.py
What is you NAME:ZZN
What is you age:24
What is you sex:n
What is you job:it
Ni Yao Cha kan:zzn
Name:zzn
Age:24
Sex:n
Job:it
Two. Formatted output
#vim test.py
#!/usr/bin/env python
Name = Raw_input ("What is You name:")
Age = Raw_input ("What Is Your Age:")
Sex = Raw_input ("What is You Sex:")
Job = Raw_input ("What Is Job:")
msg = "" "
Ni Yao Cha kan:%s
name:%s
age:%s
sex:%s
job:%s
""% (name,name,age,sex,job)
Print msg
#python test.py
Ni Yao Cha kan:zzn
Name:zzn
Age:21
Sex:n
Job:it
PS: string Common functions:
@ Remove white space or remove specified string
#vim test1.py
#!/usr/bin/env python
Name = Raw_input ("What is You name:"). Strip ("Z") #移除指定字符串 "Z"
Age = Raw_input ("What Is Your Age:"). Strip ()
Print Name,age
#python test1.py
What is you NAME:ZZN
What is you age:21 #21前面是有空格 but the output has no space
n 21
7. List
Dir (list) View list can be used with the parameters Help (list) to see the detailed assistance ' append ' (add), ' count ' (statistics), ' extend ' (iterator), ' Index ' (index), ' Insert ' (insert), ' Pop ' (delete), ' Remove ' (specify delete), ' reverse ' (invert), ' sort ' (sort) practice:>>> list = [' zzz ', ' xxx ', ' CCC ']>>> list[' zzz ', ' xxx ', ' CCC '] by index view value >>> list[1] ' xxx ' add value to list last >>> list.append ("eee") >>> list[' zzz ', ' xxx ', ' CCC ', ' eee ']>>> list.append ("CCC") >>> list[' zzz ', ' xxx ', ' CCC ', ' Eee ', ' CCC '] View the index of a value >>> List.index ("CCC") 2 statistics a value has several >>> list.count ("CCC") 2 inserted into the specified position >>> List.insert (2, "RRR") >>> list[' zzz ', ' xxx ', ' RRR ', ' CCC ', ' Eee ', ' CCC '] Delete list last value >>> List.pop () ' CCC ' >>> list[' zzz ', ' xxx ', ' RRR ', ' CCC ', ' eee '] Delete the specified value >>> list.remove ("RRR") >>> list[' zzz ', ' xxx ', ' CCC ', ' Eee ' ] Turn all values upside down >>> list.reverse () >>> list[' eee ', ' CCC ', ' xxx ', ' zzz ']>>> list.append ("!") >>> list.append ("@") >>> list.append ("#") >>> List.appenD ("$") >>> List.append (one) >>> list.append >>> list.append (19) Sort Precedence number followed by Universal character Then sort alphabetically python2.x and 3.x have different 3.x characters and numbers cannot be sorted together >>> list.sort () >>> list[11, 14, 19, '! ', ' # ', ' $ ', ' @ ', ' CCC ', ' eee ', ' xxx ', ' zzz '] extension list >>> list.extend ("eee") >>> list[11, 14, 19, '! ', ' # ', ' @ ', ' CCC ', ' eee ', ' xxx ', ' zzz ', ' e ', ' e ', ' e ' if more than 5,000 values or more how to remove a value all in the list >>> list.append ("$") >>> List.append ("$") >>> List.append ("$") >>> for-N in range (List.count (' $ ')): ... list.remove ("$") ... >>> list[11, +, +, '! ', ' # ', ' @ ', ' CCC ', ' eee ', ' xxx ', ' zzz '] slices >>> d = range (Ten) >> ;> d[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Print out the top five >>> d[0:5][0, 1, 2, 3, 4] even >>> d[0:5:2][0, 2, 4] cut the last two >> > d[-1:] [49]>>> d[-2:][48, 49] contains #可以看这个值是否在这个列表中 >>> in Dtrue 8. Tuple definitions cannot be modified after the tuple list is converted to each other >>> a[1, 2, 3, 4, 5, ' d ',' B ']>>> type (a) <class ' list ' >>>> B = (1,2,3,4,5,6) >>> B (1, 2, 3, 4, 5, 6) >>> Typ E (b) <class ' tuple ' >>>> tuple (a) (1, 2, 3, 4, 5, ' d ', ' B ') 9. Operators can set the color and and on the front end by modulo or or not non-member operator in not in>>> ' zzn ' in name true>>> ' VVV ' in namefalse>>> "VVV" not in Nametrue identity operator is was not>>> x = 1>>> x is 1true>>> 6 is xfalse>>> 6 is not xtrue
10. File operation
Files of three modes file (' Test.txt ', ' R ') r Read Only W write a append w+ readable writable no this file in read mode will error >>> file (' Test.txt ', ' R ') Traceback (most RE Cent call last): File "<stdin>", line 1, in <module>ioerror: [Errno 2] No such File or directory: ' Test.txt ' & gt;>> Write files >>> file (' Test.txt ', ' W ') <open file ' test.txt ', Mode ' W ' at 0x7ff3d27e6d20>>>> Fi Le (' Test.txt ', ' W '). Write (' df ') defines a variable >>> f = file (' Test.txt ', ' W ') >>> f.write (' The Good is very \ n ') off >>> F.close () # cat testhow is read file >>> f = file (' Test ', ' R ') >>> F.read () ' How a Re you \ n ' This is my first time to write a blog, if the writing is not good or everyone has any suggestions, can comment, I will correct!
The first day of Python learning