python代码进一步学习
Posted hjl123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python代码进一步学习相关的知识,希望对你有一定的参考价值。
1、变量的三大类型
import time list1 = [‘tank‘, ‘jason‘] # print(‘18岁的tank,是真的。‘) # type print(type(list1)) # id print(id(list1)) # 4648825800 # 4620657608 time.sleep(5) print(‘程序结束,list1被销毁‘) # 可变与不可变 # 当数据修改时,id不变称之为可变类型,否则称之为不可变类型。(默写) # 可变 list1 = [‘tank‘, ‘jason‘] print(‘list1修改前ID:‘, id(list1)) list1.append(‘徐峰‘) # [‘tank‘, ‘jason‘].append(徐峰) print(list1) print(‘list1修改后ID:‘, id(list1)) # 不可变:元组 tuple1 = (1, 2, 3) print(id(tuple1)) print(type(tuple1)) tuple1 = (2, 3, 4) print(id(tuple1))
2、用户与程序交互
‘‘‘ 用户与程序交互: input() 输入 print() 输出 username = input(‘请输入提示信息:‘) ‘‘‘ username = input(‘请输入用户名:‘) print(username) password = input(‘请输入密码:‘) print(password) # 如果 # username == ‘tank‘ True # and: 左右两边的条件都为True才为True,否则为False # password == ‘123‘ True # if 条件判断:如果条件判断 == True, 执行if下的代码 if username == ‘tank‘ and password == ‘123‘: # False and True ---> False print(‘登陆成功‘) else: print(‘登录失败‘)
3、格式化输出
# 占位符:%s str1 = ‘很帅‘ print(‘tank%s‘ % (str1,)) #tank很帅 str2 = ‘信念与我同在,我将无坚不摧!‘ print(‘自信养成记1: %s‘ % (str2,)) price1 = input(‘请输入该用户当月扣费月租:‘) all = input(‘请输入该用户当前余额:‘) print(‘亲爱的移动用户,您当月月租为[%s]元,账户余额为[%s]元!‘ % (price1,all))
4、基本运算符
# 算术运算符 print(1 + 1) n1 = 1 # n1 = n1 + 1 # n1 += 1 # print(n1) n1 += 1 # 赋值运算符:-= print(10 - 1) # *= print(12 * 12) print(11 / 3) print(11 // 3) print(11 % 3) # 比较运算符 print(1 == 2) print(1 > 2) print(1 < 2) print(1 >= 2) print(1 <= 2) # is:比较两个变量的id是否相等 x = 10 print(id(x) == id(x)) print(x is x) # 逻辑运算符 # and:左右两边的条件都为了True才为True,否则为False print(x == 1 and x == 10) print(x == 10 and x == 10) # or:判断两边的条件是否成立,其1成立则为True print(x == 10 or x == 1) # not:取反 print(x == 10) print(not x == 10) # no-->and-->or # True or False print(not x == 1 or x == 10 and x == 20) # 链式赋值 n = 10 y = n z = y print(n, y, z) # 交叉赋值 x = 10 y = 20 # x与y的值互换 # x = 20 y = 10 z = x # 10 x = y # 20 y = z # 10 x = 10 y = 20 # 10, 20 = y=10, x=20 x, y = y, x print(x, y) # 解压缩赋值 # 0 1 2 list1 = [1, 2, 3] x = list1[0] y = list1[1] z = list1[2] print(x, y, z) x, y, z = list1 print(x, y, z)
5、流程控制
‘‘‘ if 分支 ‘‘‘ # 猜字符游戏:猜测用户输入的字符是否是9527。 number = 9527 guess = input(‘请输入猜测数字:‘) print(guess) print(type(guess)) # str # 把字符串类型转为整型 guess = int(guess) print(type(guess)) #int if guess == number: print(‘猜对了!‘) elif guess < number: print(‘猜小了‘) else: print(‘猜大了!‘) ‘‘‘ while循环: 语法: while 判断条件: # 若条件成立则“循环”执行以下代码 print(‘111‘) break:结束本层循环 ‘‘‘ # import time # while True: # print(11111) # time.sleep(1) # break # 猜字符游戏: 猜测用户输入的字符是否是9527,限制只能猜3次。 number = 9527 init = 1 # 1 2 3 4 while True: # 若init大于3结束循环 if init > 3: print(‘猜错3次了,gun吧!‘) break guess = input(‘请输入猜测数字:‘) guess = int(guess) if guess == number: print(‘猜对了!‘) break elif guess < number: print(‘猜小了‘) else: print(‘猜大了!‘) # 若没有退出循环证明猜错了,init+1 init += 1 # 0 1 2 3 4 # list1 = [1, 2, 3, 4, 5] ‘‘‘ 需求: 循环取出列表list1的每一个值。 for循环: for x in 需要循环的值: for x in list1: # x=1, x=2, x=3, x=4, x=5 print(x) ‘‘‘ list1 = [1, 2, 3, 4, 5] # list1_len = len(list) # 5 # while list1_len > 0: # list1_len -= 1 # # print(list1_len) # 4 3 2 1 0 # print(list1[list1_len]) # 依次循环取出list1中的每一个值 for x in list1: print(x)
以上是关于python代码进一步学习的主要内容,如果未能解决你的问题,请参考以下文章
《Python机器学习基础教程》中英文PDF及代码+原版+高清+穆勒+张亮