0基础 python day 2

1、昨日内容回顾

(1)编译型:一次性将全部代码编译成二进制文件。 C C++

  优点:执行效率高

  缺点:开发速度慢,不能跨平台

(2)解释性:当程序运行时,从上至下一行一行的解释成二进制

  优点:开发速度快,效率高,可以跨平台。

  缺点:运行效率低。

(3)python2X 和 python3X 宏观上的区别: 

  python2X源码,重复率高,不规范,而且python崇尚的是简单优美清晰,所以龟叔创建了python3,规范化。 

  在python首行:#-*-encoding:utf-8 -*- 解决python2中文报错问题

(4)变量:由数字、字母、下划线组成,不能以数字开头;具有可描述性;不能用python中的关键字;不能用中文。

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

(5)常量:约定俗成 不可更改,全部都是大写字母。

(6)注释:单行注释用#;多行注释用'''  '''      """   """;

(7)用户交互input: 数据类型全都是str;

          基础数据类型:bool  True  False

                 int  + - * / ** % 

                 str:加引号就是str 可加,可与数字相乘

(8)if 条件:  结果

if 条件:
  结果 if 条件: 结果 else: 结果 if 条件: 结果 elif 条件: 结果 elif 条件: 结果 ... ... else:结果


if 条件:
  if 条件: 结果
    if....
  slse:结果



while 条件:
  结果
 # 终止的方法 1、改变条件 2、break continue:结束本次循环,继续下一次循环。




2、pycharm的安装使用

 此类安装教学视频较多,搜索即可。

3、作业讲解:(1)使用while循环输入 1 2 3 4 5 6 8 9 10

                        (2)输出 1—100 内的奇数

                        (3)1-2+3-4+5-6+7... ...+99

        (4)用户登录(三次登陆机会)

                        (5)挺神奇,不知道看的视频咋剪的忽然多了个作业,此作业在本文知识点5之后出现。

  # 1、使用while循环输入 1 2 3 4 5 6  8 9 10
count = 0
while count < 11:
    count +=  1
    if count ==7:
        continue
    print (count)

  # 2、输出1—100 内的所有奇数
  # 方法一
count = 1
while count < 101:
    print (count)
    count +=2

  方法二
count = 1
while count<101:
     if count%2==1:
         print(count)
     count+=1


  #  3、 1-2+3-4+5......+99
sum = 0
count = 1
while count<100:
    if count % 2 ==0:
        sum = sum - count
    else:
        sum = sum + count
    count += 1
print (sum)


  # 4、用户登录(三次机会重试)
# input 心中有账号,密码 while

i = 0
while i < 3:
    username = input('请输入账号:')
    password = int (input('请输入密码:'))
    if username == '咸鱼哥' and password == 123:
        print ('登陆成功')
    else:
        print('登陆失败请重新登陆')

    i += 1

  

  # 神奇的附加作业,没有题目,只有代码。
username = "阿亮"
password = "123456"
i = o
while i < 3:
    name = input ('请输入你的用户名:')
    pad = input('请输入你的密码:')
    if  username == name and password ==pad:
        print('登陆成功')
        break
    else:
        print('登陆失败,你还有%d次登陆机会'%(2-i))
        if (2-i)==0:
  # 在result = input ('是否还想试试?Yes')行之后,直接空格会跳出循环为什么?因为如果不输入 Yes 则不能进入此行代码下的循环,会直接跳到 i+=1 行,此时 i=3 结果直接转到最后一行。

            result = input ('是否还想试试?Yes')

            if result == 'Yes':
                i = 0
                continue
    i += 1

else: print ('不要脸了你还')

  

4、格式化输出:

  %(占位符) s(字符串)d(digit 数字)

  %% 只是单纯的显示%

# 格式化输出
  # % s d 
name = input ('你的名字是:')
age = input('你的身高是:')
height = input('你的身高是:')
# 如果在输出化格式中表示单纯的%,需要在%前在加一个% msg = '我叫%s,今年%s岁,身高%s CM 学习进度为3%%' %(name,age,height) print(msg)


name = input ('请输入你的名字:')
age = input ('请输入你的年龄:')
height = input ('请输入你的身高:')
msg = '''------ %s简介 ------
Name:%s
Age:%s
height:%s
------ end ------'''%(name,name,int(age),height)
print (msg)

5、while else

    当while循环被 break打断 ,就不会执行 else 的结果

 count = 0
while count < 5:
    count +=1
  # 此循环会被中断,可将break改为pass,即可显示:循环执行完了
    if count == 3: break
    print("Loop",count)
else:
    print("循环执行完啦")
print(*------out of while loop------*)
    

 6、初始编码

  电脑的上传,还有储存的实际上都是二进制,01010101010

  美国:ascii码   00000001  8个字节  2**8=128,可以包含美国所有字母大小写及特殊字符。

8位bit == 1个字节(byte)   1024byte==1kb  1024kb==1MB  1024MB ==1GB    1024GB== 1TB 

  中国:九万字,ascii不能满足。

  为解决全球化的文字问题,创建了一个万国码,unicode

  起初:一个字节,能表示所有英文,特殊字符,数字等等;两个字节,16位表示一个中文,不够,后用4个字节,32位表示中文,但字节太多,很浪费空间,最后对unicode做了一个升级版:utf-8 用三个字节表示一个中文。2**24=16万,足够表示中文。(PS gbk 中国人创建的编码方式,只国内使用,一个中文用两个字节表示。)

7、运算符

  + - * / ** //(整除) ==(比较相等)  !=(比较不相等)  <= >= +=

  逻辑运算 not and or 

# not and or 
  # 优先级 ()> not > and > or 从左往右
print (2>1 and 1<4)
print (2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2) # True   解析:T or T or F 或有一真为真
  # 例题
print(1<2 and 3<4 or 1>2)  # T 
print(3>4 or 4<3 and 1==1)  # F
print(2>1 and 3<4 or 4>5 and 2<1)  # T 
print(1>2 and 3<4 or 4>5 and 2>1 and 9<8)  # F
print(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6)  # F
print(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6)  # F


#若print内为数字会有何结果
print (1 or 2)  # 1
print (0 or 2)  # 2
print (3 or 2)  # 3
print (0 or 100)  # 100
  # 规律:x or y 中 x Ture,则返回 x 


  # ps  int——>bool  非零准换为bool为True o转换为bool为False
print (bool(2))  # True   ps:bool的()内只能有一个数字
print (bool(-2))  # True 
print (bool(0))  # False

  # bool——>int
print (int(True))  # 1
print (int(False))  # 0


# x and y 若x为 Ture 则返回 y ,and可不记,与or 相反
print (1 and 2)  # 2
print (0 and 2)  # 0



  # 例题
print (2 or 3 or 4 or 100)  # 2
print (0 or 4 and 3 or 2)  # 3
  # 思考题
print (2 or 1<3)
print (3>1 or 2 and 2)
print (1>2 and 3 or 4 and 3<2)
 

  

猜你喜欢

转载自www.cnblogs.com/mangoorangutan/p/10863843.html