python之路第一步

判断语句:

                     if .....elif.....else.....                如果....再如果.....否则..... 

示例代码:

 1 score = int(input('score:'))
 2 
 3 if score>90:
 4     print("A")
 5 elif score>80:
 6     print("B")
 7 elif score>70:
 8     print("C")
 9 elif score>60:
10     print("D")
11 else:
12     print("不及格")

字符串的格式化输出:

示例代码:

 1 name = input('Name:')
 2 age = int(input('Age:'))
 3 job = input('Job:')
 4 salary = input('salary:')                  #salary是薪资的意思
 5 
 6 if salary.isdigit():                       #判断salary 是否为数字
 7     salary = int(salary)
 8 else:
 9     exit('salary must input digit')        #不是就停止程序并提示
10 
11 msg = '''
12 -------inf of %s-------
13 Name:%s
14 Age:%s
15 Job:%s
16 Salary: %s
17 You will be retired in %s years
18 --------end---------
19 ''' % (name,name,age,job,salary,55 - age)   #int 不能和 str运算,要将第二行的age转为int
20 
21 print(msg)

 常用的占位符:%s   s = string

                                 %d   d = digit     整数

                                 %f    f = float     浮点数,绝大多数就是表示小数

扫描二维码关注公众号,回复: 1938419 查看本文章

万恶的字符串拼接:

name = "king"
age = "22"

print('my name is',name,'and  I am',age,'years old') 

print('my name is'+name+'and  I am'+age+'years old')  #字符串可以+* ,但不可以-,但是+会开辟五块内存,不建议用+

循环语句:

    有限循环:

1 for i in rang(3):      #循环三次
2     print("loop:",i)
1 #打印1-100中所有的奇数
2 
3 for i in rang(1,101):
4     if i % 2 ==1:
5         print(i)
6 
7 
8 for i in rang(1,101,2):             #2代表步长
9     print(i)
1 #1-100中50-70不打印
2 
3 for i in rang(100):
4     if i < 50 or i > 705         print(i)
 1 #登录输入,限制次数3次
 2   
 3 _user = "king"
 4 _password = "1234"
 5 
 6 passed_authentication = Flase         
 7 
 8 for i in range(3):
 9     username = input("Username:")
10     password = input("Password:")
11     if username == _user and password == _password:
12         print("welcome %s login ..." % _user)
13         passed_authentication = Ture 
14         break                                             #跳出,中断
15     else:
16         print("Invalid username or password !")
17 
18 if not passed_authentication: 

19 print("time is over")

 1 #登录输入,限制次数3次
 2   
 3 _user = "king"
 4 _password = "1234"
 5 
 6         
 7 
 8 for i in range(3):
 9     username = input("Username:")
10     password = input("Password:")
11     if username == _user and password == _password:
12         print("welcome %s login ..." % _user)
13          
14         break                                             #跳出,中断,打断
15     else:
16         print("Invalid username or password !")
17 
18 else:                                                     #只要上面的循环语句能正常执行完,就执行else语句,如果不能执行完就不执行下面的语句。                                     
19     print("time is over")

无限循环:

    while 循环:

 1 #登录输入,限制次数3次
 2 
 3 _user = "king"
 4 _password = "1234"
 5 
 6 counter = 0
 7 
 8 while counter < 3:
 9     username = input("Username:")
10     password = input("Password:")
11     if username == _user and password == _password:
12         print("welcome %s login ..." % _user)
13         break                                             #跳出,中断,打断
14     else:
15          print("Invalid username or password !")
16 
17     counter += 1
18 
19 else:                                                     #只要上面的循环语句能正常执行完,就执行else语句,如果不能执行完就不执行下面的语句。
20      print("time is over")
#登录输入,限制次数3次

_user = "king"
_password = "1234"

counter = 0

while counter < 3:
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
        print("welcome %s login ..." % _user)
        break                                             #跳出,中断,打断
    else:
         print("Invalid username or password !")

    counter += 1
    if counter == 3:                                      #提供选择是否重新开始
        choice = input("would you want again:[y/n]")
        if choice == "y":
            counter = 0


else:                                                     #只要上面的循环语句能正常执行完,就执行else语句,如果不能执行完就不执行下面的语句。
     print("time is over")
 1 #countine和break的区别:
 2 
 3 exit_flag = False                        #标志位
 4 
 5 for i in range(10):
 6     if i < 5:
 7         continue                         #countine跳过这次运算,继续执行运算
 8     print(i)
 9     for j in range(10):
10         print("第二",j )
11         if j == 6:
12             exit_flag = True
13             break                        #break是跳出当前级别的这个循环
14     if exit_flag :
15         break

数据类型:

列表:

增删改查

 1 A = ['a','b','c','d','e','f']
 2 
 3 #
 4 print(A[1:])         #从第二个取到最后
 5 print(A[1:-1])       #从第二个取到倒数第二个
 6 print(A[1:-1:1])     #从第二个一个一个取取到倒数第二个
 7 print(A[1::2])       #从第二个二个二个取取最后
 8 print(A[3::-1])      #如果步长为负数就是从右往左取,从左往右数第四个开始取。
 9 print(A[-2::-1])
10 
11 #添加  append insert
12 A.append('g')      #默认插到最后的位子
13 A.insert(2,'p')    #插入到索引为2的位置
14 
15 #修改
16 
17 A[1] = '2'      #将索引为1的修改为2
18 A[1:3] = ['4','5']  #将第二个和第三个改为4和5
19 
20 #删除   remove pop del
21 
22 A.remove('a')    #括号里一定要放元素,不能放索引,删除a这个元素
23 A.pop(2)         #提取索引的元素,并且可以提取。
24 del A[1]         #删除元素
25 del A            #删除整个
 1 # count 计算所查找的元素出现的次数
 2 a = [1,2,3,4,5,6,12,3,4,5]
 3 print(a.count(3))
 4 
 5 #extend 追加
 6 a = [1,2,3]
 7 b = [3,4,5]
 8 a.extend(b)
 9 print(a)
10 
11 #index 查找元素的位置
12 a = [1,2,3,4,5,6,7]
13 print(a.index(4))
14 
15 #reverse  将顺序倒转
16 a = [1,2,3,4,5,6]
17 a.reverse()
18 print(a)
19 
20 #sort  排序
21 a = [5,7,1,4,8,3]
22 a.sort()
23 print(a)

作业:购物车小程序

 1 your_money = 5000
 2 bag = []
 3 a = ['iphone6s','mac_book','coffee','python_book','bicycle']
 4 h = ['iphone6s','mac_book','coffee','python_book','bicycle']
 5 d = your_money
 6 a[0] = 5800
 7 a[1] = 9000
 8 a[2] = 32
 9 a[3] = 80
10 a[4] =1500            #赋值
11 
12 iphone6s = 5800
13 mac_book = 9000
14 coffee = 32
15 python_book = 80
16 bicycle = 1500
17 
18 have_people = True
19 
20 while have_people:
21     b = int(input('请输入购买的商品:'))
22     c = b - 1
23     d = d - int(a[c])
24     if d < 0:
25         print('余额不足',d)
26     else:
27         bag.append(h[c])                                      #将已买商品加入到bag中
28         print('已加入到bag中,当前余额',d)
29         choice = input('您是否要继续购买:y/n')
30         if choice == 'y':
31             continue
32 
33         else:
34             print('您购买了以下商品:',bag)
35             print('您的余额:',d)
36             print('欢迎下次光临')
37             break

 

猜你喜欢

转载自www.cnblogs.com/9527think/p/9270286.html