day1——变量,if语句 | day2——while循环


变量
变量的命名规则:
1,有字母,数字,下划线组成
2,不能用纯数字,也不能是数字开头
3,不能是Python的关键字;
4,不要太长(规范)
5,要有意义,具有可描述性(规范)
6,区分大小写(规范)
7,不要是中文(规范)
8,推荐使用驼峰或者下划线命名(规范)
基本类型:
int:整型
str:字符串
bool:只能用来条件的判断


用户交互
input:
input接收的内容是str


if语句
语法规则:
if 条件判断:
代码块1
else:
代码块2
执行流程:
如果条件为真,执行代码块一,否则执行代码块2

while循环

break :直接退出本层循环
continue:停止当前循环,继续执行下一次循环

区别:
break:是彻底的停掉当前循环
continue:停止当前循环,继续执行下一次循环

while...else...——break不会触发else的执行,直接彻底停止这个循环
flag = True
while flag:
    content = input('输入内容,(q退出)')
    if content == 'q':
        flag = False
    print(content)




格式化输出
%s 占位 占位的是字符串,全能的 也可以接受数字(掌握)
%d 占位 只能接受数字(了解)
name= 'alex'
age = '38'
hobby = ''
location = '湖边'
print('%s,%s,%s,%s' % (name,age,hobby,location))


如果字符串中出现了%s这样的格式化内容,后面的%都认为是格式化,
如果想要使用%,需要转义 %%

逻辑运算
顺序:
() > not > and > or

4,编码初识


初始密码本:ascii    字母, 数字, 特殊字符


0000 0001  8位==1个字节一个字节表示一个字符。


字符;组成内容的最小单元。  -------->abc     a   b    c


                                 中国     中     国


a  01100001


b  01100010


c  01100011


万国码:unicode


创建初期 16位 两个字节表示一个字符。


a :01100001 01100001


中:01100011 01100001




升级:32位 四个字节表示一个字符。


            a :01100001 01100001 01100001 01100001


            中:01100011 01100001 01100011 01100001


            资源浪费。


对Unicode升级 :utf-8。


         utf-8:最少用8位数去表示一个字符。


            a:01100001(字母用1个字节表示。)


            欧洲文字:01100001 01100001(欧洲用2个字节表示。)


            亚洲文字——中:01100001 01100001 01100001 (亚洲用3个字节表示。)


        utf-16:最少用16位数去表示一个字符




    gbk:国家标准。


        a : 01100001

        中: 01100001 01100001




    8位  1个byte  

  8bit          1byte

    1024bytes  1kb


    1024kb   1MB


    1024MB   1GB


    1024GB   1TB




day1——作业
 1、简述变量命名规范
        1,有字母,数字,下划线组成
        2,不能用纯数字,也不能是数字开头
        3,不能是Python的关键字;
        4,不要太长(规范)
        5,要有意义,具有可描述性(规范)
        6,区分大小写(规范)
        7,不要是中文(规范)
        8,推荐使用驼峰或者下划线命名(规范) 

2、name = input(“>>>”) name变量是什么数据类型?

str


3.if条件语句的基本结构?

 1  2 if 条件:
 3     代码块
 4 
 5  6 if 条件:
 7     代码块
 8 else 9     代码块
10 
11 12 if 条件:    
13     代码
14 elif 条件:
15     代码块
16 else17     代码块


4.⽤print打印出下⾯内容:
⽂能提笔安天下,
武能上⻢定乾坤.
⼼存谋略何⼈胜,
古今英雄唯是君.

1 a = '''
2 ⽂能提笔安天下,
3 武能上⻢定乾坤.
4 ⼼存谋略何⼈胜,
5 古今英雄唯是君.
6 '''
7 print(a)


5.利⽤if语句写出猜⼤⼩的游戏:
设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
正确。

1 a = 66
2 b = int(input('请输入数字'))
3 if b > a:
4     print('太大了,啊啊啊')
5 elif b < a:
6     print('你是真小啊')
7 else:
8     print('哈哈就是你了')

6.提⽰⽤户输⼊他的年龄, 程序进⾏判断.
如果⼩于10, 提⽰⼩屁孩, 如果⼤于10, ⼩于 20, 提⽰⻘春期叛逆的⼩屁孩.
如果⼤于20, ⼩于30. 提⽰开始定性, 开始混社会的⼩ 屁孩⼉, 如果⼤于30, ⼩于
40. 提⽰看⽼⼤不⼩了, 赶紧结婚⼩屁孩⼉. 如果⼤于40, ⼩ 于50. 提⽰家⾥有个
不听话的⼩屁孩⼉. 如果⼤于50, ⼩于60. 提⽰⾃⼰⻢上变成不听 话的⽼屁孩⼉.
如果⼤于60, ⼩于70. 提⽰活着还不错的⽼屁孩⼉. 如果⼤于70, ⼩于 90. 提⽰⼈
⽣就快结束了的⼀个⽼屁孩⼉. 如果⼤于90以上. 提⽰. 再⻅了这个世界.

 1 age = int(input('请输入你的年龄'))
 2 if age < 10:
 3     print('小屁孩')
 4 elif 10 <= age < 20:
 5     print('⻘春期叛逆的⼩屁孩')
 6 elif 20 <= age < 30:
 7     print('开始定性, 开始混社会的⼩ 屁孩⼉')
 8 elif 30 <= age < 40:
 9     print('⽼⼤不⼩了, 赶紧结婚⼩屁孩⼉')
10 elif 40 <= age < 50:
11     print('家⾥有个不听话的⼩屁孩⼉')
12 elif 50 <= age < 60:
13     print('⾃⼰⻢上变成不听 话的⽼屁孩⼉')
14 elif 60 <= age < 70:
15     print('活着还不错的⽼屁孩⼉')
16 elif 70 <= age < 90:
17     print('就快结束了的⼀个⽼屁孩⼉')
18 elif age > 90:
19     print('再⻅了这个世界')



7、单⾏注释以及多⾏注释?
# 和 '''被注释内容'''


8、提⽰⽤户输⼊⿇花藤. 判断⽤户输⼊的对不对. 如果对, 提⽰真聪明, 如果不
对, 提⽰你 是傻逼

content = input('输⼊⿇花藤')
if content == '⿇花藤':
    print('真聪明')
else:
    print('你是傻逼')



9. ⽤户输⼊⼀个⽉份. 然后判断⽉份是多少⽉. 根据不同的⽉份, 打印出不同的
饮⻝(根据个⼈习惯和⽼家习惯随意编写)
mouth = int(input('请输入月份:'))
if mouth == 1:
    print('傻逼')
if mouth == 2:
    print('大大傻逼')
if mouth == 3:
    print('大大大傻逼')
if mouth == 4:
    print('大大大大傻逼')
if mouth == 5:
    print('大大大大大傻逼')
if mouth == 6:
    print('大大大大大大傻逼')
if mouth == 7:
    print('大大大大大大大傻逼')
if mouth == 8:
    print('大大大大大大大大傻逼')
if mouth == 9:
    print('大大大大大大大大大傻逼')
if mouth == 10:
    print('大大大大大大大大大大傻逼')
if mouth == 11:
    print('大大大大大大大大大大大傻逼')
if mouth == 12:
    print('大大大大大大大大大大大大傻逼')


10. ⽤户输⼊⼀个分数. 根据分数来判断⽤户考试成绩的档次,
          >=90      A
          >=80      B
          >=70      C
          >=60      D
          < 60      E


score = int(input('请输入成绩:'))
if score >= 90:
    print('A')
elif 90 > score >= 80:
    print('B')
elif 80 > score >= 70:
    print('C')
elif 70 > score >= 60:
    print('D')
elif score < 60:
    print('E')
day2——作业


Day2作业及默写
1、判断下列逻辑语句的True,False.
1print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
——T
2print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
——F

2、求出下列逻辑语句的值。
1),
print(8 or 3 and 4 or 2 and 0 or 9 and 7)
——8
2),
print(0 or 2 and 3 and 4 or 6 and 0 or 3)
——4

3、下列结果是什么?
1)、
print(6 or 2 > 1)
——6
2)、
print(3 or 2 > 1)
——3
3)、
print(0 or 5 < 4)
——False
4)、
print(5 < 4 or 3)
——3
5)、
print(2 > 1 or 6)
——True
6)、
print(3 and 2 > 1)
——True
7)、
print(0 and 3 > 1)
——0
8)、
print(2 > 1 and 3)
——3
9)、
print(3 > 1 and 0)
——0
10)、
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
——2

4、while循环语句基本结构?

while 1:
    pass
else:
    passwhile 条件:
    循环体

    如果条件为真,则循环体执行
    如果条件为假,则循环体不执行
②
while 条件:
    循环体

else:
    代码块
    (else作用是:当while循环正常执行完,中间没有break
    终止的话,就会执行else后面的语句)



5、利⽤while语句写出猜⼤⼩的游戏:
设定⼀个理想数字⽐如:66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
正确,然后退出循环。

a = 66
while True:
    content = int(input('请输入数字:'))
    if content > a:
        print('啊啊啊,太大了')
    elif content < a:
        print('太小了')
    else:
        print('good')
        break


6、在5题的基础上进⾏升级:
给⽤户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。

i = 3
a = 66
while True:
    content = int(input('请输入数字(您有%s次机会):'% i))
    if content > a:
        print('啊啊啊,太大了')
    elif content < a:
        print('太小了')
    else:
        print('good')
        break
    i -= 1
    if i == 0:
        print('太笨了你')
        break



7.使⽤while循环输⼊ 1 2 3 4 5 6 8 9 10
①
count = 1
while count <= 10:
    if count != 7:
        print(count)
    count += 1



count = 1
while count <= 10:
    if count == 7:
        count += 1
    print(count)
    count += 1


8.求1-100的所有数的和

count = 1
sum = 0
while count <= 100:
    sum += count
    count += 1
print(sum)


9.输出 1-100 内的所有奇数

count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count +=1



10.输出 1-100 内的所有偶数

count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1


11.求1-2+3-4+5 ... 99的所有数的和.

sum = 0
count = 1
while count <= 99:
    if count % 2 == 0:
        sum -= count
    else:
        sum += count
    count += 1
print(sum)


12.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)

①
count = 1
while count <= 3:
    username = input('>>>').strip()
    password = input('>>>').strip()
    if username == 'alex' and password =='123':
        print('good')
        break
    else:
        print('sorry,you  are wrong')
        print('还剩下%s次机会'%(3 -count))
    count += 1



②
i = 3
while True:
    name = input('请输入姓名:')
    pwd = input('请输入密码:')
    if name == '李振楠' and pwd == 'nizhenshuai':
        print('good')
        break
    else:
        print('账号或者密码错误,你还有%s次机会'%(i-1))
    i -= 1
    if i == 0:
        print('登录失败')
        break


13. ⽤户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题).
'''
思路:
质数只能被1和自身整除,让这个数从2开始除,一直除到n-1
如果除开了,一定不是质数,到最后还没有除开,就是质数!

'''
课上讲解
n = int(input('>>>'))
if n == 1:
    print('不知道是不是')
else:
    count = 2
    while count <= n-1:
        if n % count == 0:
            print('no')
            break
        count += 1
    else:
        print('good')

①
count = int(input('>>>'))
if count <= 1:
    print('这不是一个质数')
elif count == 2:
    print('这是一个质数')
else:
    i = 2
    while i < count:
        if count % 2 == 0:
            print('这不是一个质数')
            break
        i += 1
    else:
        print('这是一个质数')

②
num = int(input('请输入数字:'))
for i in range(2,num):
    # print(i)
    if num % i == 0:
        print('这不是一个质数啊')
        break
else:
    print('good')



14. 输⼊⼀个⼴告标语. 判断这个⼴告是否合法. 根据最新的⼴告法来判断. ⼴
告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包
含. 提示, ⼴告不合法
例如, 1. ⽼男孩python世界第⼀. ==> 不合法
   2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法
①
content = input('输入:')
if '' in content or '第一' in content or '稀缺' in content or '国家级' in content:
    print('不合法')
else:
    print('合法')


②
content = input('输入标语:')
list = ['','第一','稀缺','国家级']
for i in list:
    if i in content:
        print('不合法')
        break
else:
    print('合法')




14. 输⼊⼀个数. 判断这个数是⼏位数(⽤算法实现)(升级题)
①
num = int(input('请输入一个数:'))
i = 0
while num  >= 1:
    num = num // 10
    i += 1
print('这个数是%s位数'% i)

②
num = input('>>>')
count = 0
while count < len(num):
    count += 1
print('这是一个%s位数'% count)

③
num = input('请输入数字:')
print('这是一个%s位数'% (len(num)))


原文地址:https://www.cnblogs.com/-li926/p/9469723.html