随笔分类 - python 基础
python base
摘要:print(list(range(10))) # 从0开始到10 print(list(range(6,11))) # 从6开始到10 print(list(range(1,10,2))) # 从1到10之间,取步长为2的值 print(list(range(10,0,-1))) # 步长-1,倒数。 sum100 = 0 for i in range(1,101): # pr...
阅读全文
摘要:astr = 'hello' alist = [10,20,30] atuple = ('bob','tom','alice') adict = {'name':'john','age':23} for ch in astr: print(ch) for i in alist: print(i) for name in atuple: print(name) fo...
阅读全文
摘要:计算100以内偶数之和。 是余数的话就执行continue,跳过本次循环剩余部分。是偶数的话就直接执行 sum100 += conuter 进行累加 sum100 = 0 counter = 0 while counter < 100: counter += 1 # if counter % 2 == 1: continue sum100 += ...
阅读全文
摘要:break是结束循环,break之后、循环体内代码不再执行。
阅读全文
摘要:import random num = random.randint(1,10) counter = 0 while counter num: print('猜大了') elif answer < num: print('猜小了') else: print('猜对了') break counter += ...
阅读全文
摘要:import random num = random.randint(1,10) running = True while running: answer = int(input('guess the number: ')) if answer > num: print('猜大了') elif answer < num: print('猜...
阅读全文
摘要:去掉重复的代码,结果输出字体增加颜色。 import random all_choice = ['石头','剪刀','布'] win_list = [['石头','剪刀'],['剪刀','布'],['布','石头']] prompt = '''(0) 石头 (1) 剪刀 (2) 布 请选择(0/1/2): ''' computer = random.choice(all_choice) ind ...
阅读全文
摘要:import random all_choices = ['石头','剪刀','布'] computer = random.choice(all_choices) player = input('请出拳: ') print('Your choice:', player, "Computer's choice:", computer) # 另外一种写法: # print("Your choic...
阅读全文
摘要:score = int(input('分数: ')) if score >= 60 and score = 90: print('优秀') else: print('你要努力了') 输出:
阅读全文
摘要:score = int(input('分数: ')) if score >= 90: print('优秀') elif score >= 80: print('好') elif score >= 70: print('良') elif score >= 60: print('及格') else: print('你要努力了') 输出:
阅读全文
摘要:import random num = random.randint(1,10) # 随机生成1-10之间的数字。 answer = int(input('guess a number: ')) # 将用户输入的字符转成整数。 if answer > num: print('猜大了') elif answer < num: print('猜小了') else: pri...
阅读全文
摘要:import getpass # 导入模块 username = input('username: ') # getpass模块中,有一个方法也叫getpass password = getpass.getpass('password: ') if username == 'bob' and password == '123456': print('Login successful...
阅读全文
摘要:a = 10 b = 20 if a < b: smaller = a else: smaller = b print(smaller) s = a if a < b else b # 和上面的if-else语句等价的。 print(s) 输出:
阅读全文
摘要:单个的数据也可以作为判断条件。 任何值为0的数字,空对象都是False,任何非0数字,非空对象都是True。
阅读全文
摘要:# 字典是key-value(键 - 值) 对形式,没有顺序,通过键取出值 adict = {'name':'bob','age':30} print(len(adict)) print('bob' in adict) # False print('name' in adict) # True print(adict) adict['email'] = 'aaa@qq.com' adict[...
阅读全文
摘要:列表也是序列对象,但它是容器类型,列表中可以包含各种数据。
阅读全文
摘要:在Python中,单双引号没有区别,表示的含义是一样的。 sentence = 'tom\'s pet is a cat' # 单引号中间还有单引号,可以转义。 sentence2 = "tom's pet is a cat" # 也可以用双引号包含单引号。 sentence3 = "tom said:\"hello world!\"" sentence4 = 'tom said:"hell...
阅读全文

浙公网安备 33010602011771号