第二天python

1.pycharm的安装:

       1.先去官网下载软件:https://www.jetbrains.com/pycharm/download/#section=windows然后进行下一步,下一步操作既可以完成(需要提醒的时使用pycharm)      必须使用python  环境)激活可以使用软件管家公众号里的方法。

      2.location  设置自己程序保存位置*(后续写文件时只需要改最后的文件名即可);interpreter把自己python的安装路径放进去即可;再点击create之后在点击file       New   pythonfile 再起名字就可以了。    成功案列:

 2.使用while循环输入1 2 3 4 5 6 8 9 10

  第一种方法:

   
count=0
while count<10:
    count+=1
    if count==7:
        continue
    print(count)
结果是
1
2
3
4
5
6
8
9
10
View Code

   第二种方法:

   
count=0
while count<10:
    count+=1
    if count==7:
       pass
    else:
        print(count)
结果是:
1
2
3
4
5
6
8
9
10
View Code

3求1-100所有数的和:

  第一种方法:

count=1
sum1=0
while count<101:
    sum1+=count
    count+=1
print(sum1)
结果是:
5050
View Code

   第二种方法:

count=1
sum1=0
while True:
    sum1+=count
    count+=1
    if count>100:
        break
print(sum1)
结果是
5050
View Code

4 输出1-100内所有的奇数:

   方法一:

count=1
while count<100:
    print(count)
    count+=2
结果是:
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
View Code

 方法二

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

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

第一种方法:

count=1
flag=1
sum1=0
while count<100:
    sum1+=flag*count
    flag=-flag
    count+=1
print(sum1)
View Code

第二种方法:

count=1
flag=1
sum1=0
while count<100:
    sum1+=count*(-1)**(count-1)
    count+=1
print(sum1)
结果是:
50
View Code

6.用户登陆(三次机会重试)

 View Code

7.python的编码方式:

  电脑刚开始的编码方式是二进制,即0和1两个数字(高电平表示1,低电平表示0),而且美国在刚开使用时只是制造了ascii码,使用7位二进制进行表示,最高位用于后期文字的补充。后期随着计算机发展,中国汉字共有9万多而使用两个字节(gbk)则只能表示65536个汉字故发明了万国码(unico)使用4个字节表示。但是这个表示的数据量超过了汉字而且占据内存大,后期发明了utf-8 (使用3个汉字来表示)。

8.算术运算符

  程序如下

print(2+3)
print(3-1)
print(3*1)
print(5/2)
print(5%2)
print(5//2)
print(2**3)
结果如下:
5
2
3
2.5
1
2
8
View Code

9.比较预算符:得到的结果要不是True要不是False

 10.赋值运算符

11.逻辑运算符  :

 12优先级:在进行操作时:()>not >and >or

 13短路原则:在进行逻辑运算时:如果只有and运算只有有一个为假则终止运算此结果为假;如果只有or运算,有一个为真则结束此次运算运算结果为真。

print(3>4 or 4<3 and 1==1)
print(1<2 and 3<4 or 1>2)
print(2>1 and 3<4 or 1>3)
print(3>1 and 3<4 or 4<5 and 2<1)
print(1>2 and 3<4 or 4>5 and 2>1 or 9<8)
print(not 2>1 and 3<4 or 4<5 and 2>1 and 9>8 or 7<6)
结果是:
False
True
True
True
False
True
View Code

14如果x or y   x为True 则返回x

print(1 or 2 )
print(3 or 4)
print(0 or 2)
结果是
1
3
2
View Code

15如果x and y x为True 则返回y

print(1 and 2 )
print(3 and 4)
print(0 and 2)
结果是
2
4
0
View Code

16布尔值和数字之间的转换:

由数字转换成布尔值:只要非零数字转换成布尔值都是True,其他都是False

由布尔值转换成数字;True为1;False为0

print(bool(2))
print(bool(-3))
print(bool(0))
print(int(True))
print(int(False))
结果是
True
True
False
1
0
View Code

 17进制转换:

  1B=8bit  1Kb=1024b   1Mb=1024Kb   1Gb=1024Mb

18格式化字符串:%s(字符串)%d(数字);

name=input('请输入姓名:')
age=input('请输入年龄:')
height=input('请输入身高:')
msg='我叫%s,今年%s,身高%s'%(name,age,height)
print(msg)
结果是:
请输入姓名:小明
请输入年龄:15
请输入身高:123
我叫小明,今年15,身高123
View Code
name=input('请输入姓名:')
age=input('请输入年龄:')
job=input('请输入工作:')
hobbie=input('你的爱好:')
msg='''  -------info of %s---------
Name  :%s
Age  :%d
Job : %s
Hobbie:%s
--------end---------''' %(name,name,int(age),job,hobbie)
print(msg)
结果是:
请输入姓名:fjdskl
请输入年龄:12
请输入工作:jfs
你的爱好:jsdfk
  -------info of fjdskl---------
Name  :fjdskl
Age  :12
Job : jfs
Hobbie:jsdfk
--------end---------
View Code
原文地址:https://www.cnblogs.com/ab461087603/p/12231921.html