day1内容总结计算机基础--while循环

day1内容大纲。

1,初识计算机。CPU 内存,硬盘,操作系统

2.编程语言的分类

3,python发展史

4,Python的种类。

5,变量常量注释

6,数据类型

7,用户输入

8,if语句

9,while循环

 

 

1.初识计算机
CPU,内存,硬盘,操作系统
cpu:相当于计算机的大脑,所有运算的操作者
内存:存储或从硬盘调用数据,供CPU运算
优点:读取数度快
缺点:空间小,成本高,断电数据即消失
硬盘:存储大量数据,相当于计算机的数据库
优点:空间大,成本低,断电数据不消失
操纵系统:调配计算机的硬件
2.编程语言的分类
编译型:一次性将代码转换成二进制,然后执行
优点:执行效率高;缺点:开发效率低,不能跨平台
代表语言:C
解释型:将代码逐句转换成二进制,边解释边执行
优点:开发效率高,可跨平台;缺点:执行效率低
代表语言:Python(python有自带的第三方库,可以极大提高开发效率)
3.python的发展
python2只支持到2020年,且不再更新,应尽快移植到python3.4++
python2x与3x的宏观区别
python2x源码混乱,含有很多PHP,C,JAVA的编写陋习,且重复代码多,不符合python优雅,简单清晰的宗旨
python3x重整了源码,源码规范优雅,简单,清晰
2x与3x的小区别之print,和编码规则,数据类型
python2有python‘内容’和python(‘内容’)两种输出方式

print('word')
print 'word'

 


python3只有print(‘内容’)一种,更加规范

print'word'

 


python2默认编码规则是ASCII编码,如果要显示中文,需要在代码首行输入#-*- encoding:utf-8 -*-

#-*- encoding: utf-8 -*-

 


python3默认编码规则是utf-8,可以显示中文
python2x中有long型和int型的区别(超出整形范围会自动将整形转换为长整形),python里不再有long型全是是int型
4.python的种类
根据解释器的不同python分为不同的种类,Cpython,Jpython,其他语言的python,以及pypy
python自带官方推荐的Cpython解释器,另外pypy追求执行效率时使用,相当于编译型语言
5.变量,常量和注释
变量:用以存储中间数据,供后续代码使用
变量命名规范:
1.变量名有英文字母,数字和下划线组成
2.变量名不能以数字开头
3.变量名不能是关键字
4.变量名要有可描述行
5.变量名不能过长
6.变量名不能是中文或者拼音
变量名有下划线体和驼峰体两种书写规范,官方推荐下划线体

age_of_rain

 


常量:约定全字母大写表示常量(书写时将常量写在代码的上端)

IDNUMBER

 


注释:单行注释#,多行注释用三个单引号或者三个双引号

#单行注释
'''多行注释
多行注释
'''
注释

 


注释的作用是帮助程序员互相理解代码,或者帮助自己回忆代码
6.数据类型
1.整数类型
2.字符串型:python中所有被引号引住的全部是字符串类型,单引号双引号,和三引号(用于换行的类型)、
字符型可以进行乘法和加法运算
3.布尔型True 和False(用于条件判断)

type()方法,用于查看数据的数据类型


7.用户输入input
name = input('what's your name?')
注意input输出的数据类型是字符串类型,可以用int方法将数字组成的字符串转换为int型
8.if语句
单if:

name = input("what's your name?")
if name == 'rain':
    print('handsome')
if
name = input("what's your name?")
if name == 'rain':
    print('handsome')

 


if...else

name = input("what's your name?")
if name == 'rain':
    print('handsome')
else:
    print('try again')
if-else

 


if..elif

name = input("what's your name?")
if name == 'rain':
    print('handsome')
elif name == 'peiqi':
    print('oldboy')
elif name == 'haohao':
    print('student')
    
if-elif

注意if与elif是同级判断语句

if..elif...else

if name == 'rain':
    print('handsome')
elif name == 'peiqi':
    print('oldboy')
elif name == 'haohao':
    print('student')
else:
    print('who are you?')
    
if-elif-else

if,elif,else是线性从上往下的关系,不满足上面的条件则进行下面的判断,满足条件则不进行下面的判断(当满足多个条件时要注意)


两个if语句嵌套(可以从树状图和表格方式考虑if和while语句)

name = input("what's your name?")
if name == 'rain':
    password = int(input('your password'))
    if password == 520 :
        print('congratulate')
    else:
        print('密码错误')
else:
    print('账户错误')
if嵌套

if嵌套用于多种判断,像树状图一样判断,最终正确的方向有两重判断(两级树状图)


9.while循环
while...: \n tab

while True:
    print('where')
    print('who')
    print('go where')
    
while true

输出1到100

count = 1
while True:
    print(count)
    count += 1
    if count == 101:
        break
输出1-100

输入1-100第二种方法

count = 1
while count <=100:
    print(count)
    count += 1
输出1-100的简单方法

第一种方法行数较多,count一直在变化,用第二种count作判断条件更简便

输出1-100的偶数

count = 1
while count <=100:
    if count % 2 == 0:
        print(count)
    count += 1
    
输入偶数

注意if条件后面应该时布尔值,所以注意不能用单等号

while循环语句执行到底部是返回条件判断,为True则继续循环,为False则不再循环,执行下面后续语句
循环终止方法1:改变判断条件True改为Fasle,break

 

以下为太白博客的代码训练

1.程序交互(用户输入多个信息,并输出反馈)

name = input("what's your name?")
hometown = input('where are you from?')
hobby = input('what do you like?')
print("I'm " + name + ' come from ' + hometown + ' i like ' + hobby)

 

2.字符串的拼接和乘法

注意:双引号用于字符中存在单引号的情况,(双引号就是双引号,不是两个单引号,多引号时多个单引号)

字符串加号拼接为无隙连接,记得在字符中加入空格,字符串只能跟字符串连接,不能跟其他类型的数据拼接

name = input("what's your name?")
hometown = input('where are you from?')
hobby = input('what do you like?')
introduce = "I'm " + name + ' come from ' + hometown + ' i like ' + hobby +' '
print(introduce * 3)
字符串拼接

 

3.写个猜年龄的游戏

age = 18
age_guess = int(input('how old,you guess'))
if age_guess > 18:
    print('猜大了')
elif age_guess < age:
    print('猜小了')
else:
    print('恭喜你猜对了')
猜年龄

4.匹配成绩

score = int(input('how much is your score?'))
if score > 100:
    print('别开玩笑了')
elif score >= 90:
    print("you get 'A'")
elif score >=80:
    print("you get 'B'")
elif score >= 60:
    print("you get 'C'")
elif score >= 40:
    print("you get 'D'")
else:
    print('上帝肯定为你打开了另一扇门')
成绩匹配A--D

5.循环中止

count = 1
while count <= 100:
    print('loop',count)
    if count == 5:
        print('---loop over-----')
        break
    count += 1
循环中止

6.跳跃输出

跳跃输入有三种写法,单if,if..else(pass)和if..else(continue),单if写法最为简单

count = 0
while count <= 99:
    count += 1
    if count >= 6 and count <= 95:
        continue#结束当前循环,回到循环判断条件继续循环
    else:
        print(count)#因为有continue语句,程序可能不会执行完,为保证+1的稳定执行,将加一放在上端,调整起始值和判断条件
跳跃输出

7.while..else

循环没有被break中止则while前半部分语句执行完后会执行else语句,被break中止,则不执行else语句(else语句也属于循环),用于判断程序有没有遇到break语句的条件

count = 1
while count <= 100:
    print(count)
    if count == 18:
        break
    count +=1
else:
    print('-----loop finally over-----')
while..else

day1练习题

练习题1

count = 0
while count <= 9:
    count += 1
    if count == 7:
        continue
    else:
        print(count)
输出1-10,跳过7

练习题2

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

练习题3

count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count += 1
1-100输出奇数,跳跃输出方法1
count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    else:
        pass
    count += 1
跳跃输出方法2
count = 0
while count <= 99:
    count += 1
    if count % 2 == 1:
        print(count)
    else:
        continue
#continue方法,因为特定条件会终止当前循环,因此要将循环进行语句(count+=1)放在continue前面,同时调整count初始值,和判断条件
跳跃输出方法3

跳跃输出有3中方法,单if,if..else(else语句中使用pass),if...else(else语句中使用continue),个人感觉单if最为简单

练习题4,输出偶数,与联系题3相同,只需调整以下if的判断条件

count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1
1-100跳跃输出偶数

练习题5,1-2+3-4+5..99

count = 1
sum = 0
while count <= 99:
    if count % 2 == 1:
        sum += count
    else:
        sum -= count
    count+=1
print(sum)
#循环+条件判断
1-2+3-4+5...99

练习题6

name = 'rain'
password = 520
count = 1
while count <= 3:
    user_name = input('please input your accout')
    user_pword = int(input('please input your password'))
    if name == user_name and password == user_pword:
        print('登陆成功,欢迎使用QQ')
        break
    else:
        print('账号或密码错误,请重新输入,错误次数为:',count,'还可输入',3-count,'')
        if count == 3:
            print('您已经三次输入错误的账号或密码,账户已锁定')
            break
    count += 1
用户登陆,有3次机会

结束循环最好规范的使用break

while表格和if树状图分析流程

3次机会,用while做3次循环(表格1,2,3),

每次循环中用if进行树状判断,条件正确则输出成功信息,直接break跳出,不必三次循环,条件false则输出错误信息,再增加if树状分流,如果是第三次错误则break结束循环(也有输入5次,第三次就锁定的要求,因此要避免利用循环重复刚好是3就不加break)

 

posted @ 2018-04-26 20:34  dmyHero  阅读(233)  评论(0)    收藏  举报