Python

next:Python_2(form19)

1---------------------------install

www.python.org

 

2.-----------------------------------------------------------------------------

string use "" or '' 

\' treat ' as normal text

OK "She don't do that" = 'She don\'t do that'

WRONG: 'She don't do that'

\n:change new line

print("c:\users\norrl")

>>> print("C:\Desktop\nudePice")
C:\Desktop
udePice

r "XXX",use r :rush string, print it with out special meaning

a. use special character

b.working with file paths

 

>>> print(r"C:\Desktop\nudePice")
C:\Desktop\nudePice

variable:

tuna = 16

tuna + 7

bacon = "Hello World!"

>>> tuna = 17
>>> bacon= "Hello World"
>>> tuna +1
18
>>> print(bacon)
Hello World

multiply [ˈmʌltəˌplaɪ]vt.& vi.乘;

perform mathmatical operations on string:

string can multiply in Python

>>> firstName='JOY '
>>> firstName + 'chou'
'JOY chou'
>>> firstName * 5
'JOY JOY JOY JOY JOY '
>>> 

Python Programming Tutorial - 4 - Slicing up Strings ------------------------------------------------------------------------------------

square brackets[]

slice out a section of the name:index start from 0 in computer

user[2:7] from2 end 7(exclude)

[2:]from index 2 to the end

[:7]from start to 7(exclude)

[:]from start to the end

>>> user = 'Fish Colden'
>>> user[0]
'F'
>>> user[-1]
'n'
>>> user[2:7]
'sh Co'
>>> user[:7]
'Fish Co'
>>> user[2:]
'sh Colden'
>>> user[:]
'Fish Colden'
>>> len(user)
11
>>> len('I love to study new tech')
24
>>> 

 ---5 Lists-----------------------------------------------------------

 

创建>>> playerName = ['combi',"Aprica",'peigon']
>>> playerName[1]
'Aprica'

Combine to list ,display
>>> player+playerName
[11, 12, 16, 18, 20, 'combi', 'Aprica', 'peigon']
>>> player
[11, 12, 16, 18, 20]
>>> players = player + playerName
>>> players
[11, 12, 16, 18, 20, 'combi', 'Aprica', 'peigon']
>>> player
[11, 12, 16, 18, 20]

add new item to List
>>> player.append(333)
>>> player
[11, 12, 16, 18, 20, 333]
>>> player[:2]
[11, 12]

change content 0f index0,1
>>> player[:2]=[41,45]
>>> player
[41, 45, 16, 18, 20, 333]

remove index0,1 from list
>>> player[:2]=[]
>>> player
[16, 18, 20, 333]

clear the whole list
>>> player[:]=[]
>>> player
[]

7 if elif else-------------------------------------

main.py

name = 'DK'
if name is 'bk':
    print('this is bk')
elif name is 'luc':
    print('this is lucy speaking')
else:
    print('this is'+name)

 8 for--------------------------------------------------

foods = ['cola','potato','bacon','beef','broccoli']
for food in foods:
    print(food)
    print(len(food))
for slicex in foods[:3]:
    print(slicex)
for x in range(5):
    print(x)
for y in range(100,110):
    print(y)
for z in range(100,125,5):
    print(z)

log

C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/forStatment.py
0
1
2
3
4
100
101
102
103
104
105
106
107
108
109
100
105
110
115
120

9 while loop-------------------------------

num = 5;
while num < 10:
    print(num)
    num +=1
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/forStatment.py
5
6
7
8
9

Process finished with exit code 0

 ---10  comments and break-----------------------------------

magicNumber = 27
# single line comment:
'''mutible line comment
print("stinrg1"+"stiring2")
n=1
print(n,"is the value of n")
'''
for n in range(20,33):
    if n is magicNumber:
        print(n,"is the magic number")
        break
    else:
        print(n)
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/forStatment.py
20
21
22
23
24
25
26
27 is the magic number

Process finished with exit code 0

 

for n in range(101):
    if 0 is n % 4:
        print(n)

 

 ---11 continue----------------------------------

mum = [3, 6, 9]
for n in range(1,10):
    if n in mum:
        continue
    print(n)
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/forStatment.py
1
2
4
5
7
8

Process finished with exit code 0

 

 

 

 ---12 ,13  function and return -----------------------------------

def functoinName()

Attention: need a whitespace behind a comma,like this print('Now', usd, ' USD is about ', rmb, " RMB")

 

#Function
def beef(x):
    print(x + " is what i input!")
#beef('Apple')

#Function and return
def changeCurrency(USD):
    amount = 6.68 * USD
    return amount

usd = 100;
rmb = changeCurrency(usd)
print('Now', usd, ' USD is about ', rmb, " RMB")
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
Now 100  USD is about  668.0  RMB

Process finished with exit code 0

 

 ---14  default values of arguments----------------------------------

def get_gender(sex="Unknow"):
    if sex is 'f':
        print("input gender is female")
    elif sex is 'm':
        print("input gender is male")
    else:
        print("gender is"+sex)

get_gender("f")
get_gender('m')
get_gender()

C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
input gender is female
input gender is male
gender isUnknow

Process finished with exit code 0

  ---15  scope of variable-----------------------------------

1 define a variable

2. after 1 is done. use the variable

a = 1
def beef():
    print(a, " is a")

def changeCurrency():
    print(b + " is a")
beef()
b = 100
changeCurrency()
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
1  is a
Traceback (most recent call last):
  File "C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py", line 10, in <module>
    changeCurrency()
  File "C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py", line 7, in changeCurrency
    print(b + " is a")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Process finished with exit code 1

 



 ---16  keyword argument-----------------------------------

positional argument :sinario('NN', "taste", 'Good')

keyword argument:sinario(action = "taste")

Atention: keyword argument  can not followed by positional argument

def sinario(name = 'JOO', action = 'love',item = 'tomato'):
print(name, action, item)

sinario()
sinario('NN', "taste", 'Good')
sinario('TOO', action = "taste")
#sinario(action = "taste", 'Good')

C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
JOO love tomato
NN taste Good
TOO taste tomato

Process finished with exit code 0

 ---17 - Flexible Number of Arguments-----

amount of arg is flexible,by use " * "

def sinario(*args):
    total=0
    for i in args:
        total += i
    print("total is ", total)
sinario()
sinario(1, 3, 5)
sinario(100, 15, 34)
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
total is  0
total is  9
total is  149

Process finished with exit code 0

 - 18 - Unpacking Arguments----

unpack argument by add * before datasets

def healthy_calc(name, age, apples, cigr):
expect = 100 - age +(apples * 2) - (cigr * 2)
print(name, " in expect of age ", expect)

joy_data = ["joy", 30, 7, 20]
healthy_calc(joy_data[0], joy_data[1], joy_data[2], joy_data[3])
healthy_calc(*joy_data)
C:\Users\30478\AppData\Local\Programs\Python\Python36\python.exe C:/Users/30478/PycharmProjects/YouTubeTutorial/func1.py
joy  in expect of age  44
joy  in expect of age  44

Process finished with exit code 0

 

posted @ 2017-08-08 21:18  charles999  阅读(189)  评论(0编辑  收藏  举报