Python学习之旅_02day

点点圈 提交于 2020-03-05 03:56:27

一.回顾01day学习的东西

1.变量定义:1.1为了存储程序运算过程中的一些中间结果,为了方便日后调用

                      1.2存在一定的描述性,让大众一看就知道该变量的用途

2.变量的命名规则:

  2.1 具有描述性

 2.2 变量只能由字母,数字和下划线组成

 2.3 不能以关键字(保留字)命名

 2.4 驼峰式命名和下划线分割单词

 2.5 变量名区分大小写

 2.6 不能以中文为变量名

 2.7 不能以数字开头

3.缩进

  表示是一坨代码,利用相同的缩进表示  

4. if

a=1

b=2

if a > b:

  print("yes")

elif a == b:

  print("boom")

else:

        print("no")

5.注释

单行注释:#

多行注释:"""***""" 或 '''***'''

6.字符串的拼接

abc qwe

print('abc'+'qwe')

abcqwe

7.算术运算符

 若区分不了优先级,可使用小括号去运算"()";python不允许使用中括号'[]'区分算术等级

 1 >>> 1+2
 2 3 *加
 3 >>> 2*2
 4 4  *程
 5 >>> 3-2
 6 1 *减
 7 >>> 4/2
 8 2.0 *除
 9 >>> 5//2
10 2 *整除
11 >>> 5/2
12 2.5 *除
13 >>> 9%2
14 1 *取余
15 >>> 2**10
16 1024 *幂运算

8.类型的强制转换

>>> a=input('enter:')
enter:12
>>> a
'12'
>>> print(type(a))
<class 'str'>
>>> int(a)
12
>>> print(type(12))
<class 'int'>

 9.数值类比较

>>> a=3
>>> b=5
>>> a<b
True
>>> a>b
False
>>> a == b
False
>>> a!=b
True
>>> a<=b
True
>>> a>=b
False

10.if比较语句嵌套

max_number=0
number1 = int(input('please input the number1:'))
number2 = int(input('please input the number2:'))
number3 = int(input('please input the number3:'))

if number1 >= number2:
    max_number = number1
    if max_number >= number3:
        print ('最大值是:',max_number)
    else:
        print ('最大值是:',number3)
else : 
    max_number = number2
    if max_number >= number3:
        print ('最大值是:',max_number)
    else:
        print ('最大值是:',number3)

 

11.赋值运算符

num += 1   等价于  num = num + 1
num -= 1   等价于  num = num - 1
num *= 2   等价于  num = num * 2
num /= 2   等价于  num = num / 2
num //= 2  等价于  num = num // 2

12.条件短路原则

条件1 and 条件2   当条件1为假时,则该两个条件组成的表达式,不需要判断条件2,肯定为假

条件1 or 条件2   当条件1为真时,则该两个条件组成的表达式,不需要判断条件2,肯定为真

条件优先级:or < and < not

13.while语句:输出100以内所有偶数

num = 1

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

14.while与if嵌套语句(版本1.利用变量来终止程序;版本2.利用break来终止程序)

age = 55
flag = True
while flag:
    guess_age = int(input('please input guess_age:'))
    if guess_age == age:
        print('you are right')
        flag = False #break
    elif guess_age > age:
        print('it is bigger')
    else:
        print('it is smaller')
print('game over')

15.while与continue嵌套语句  # continue:表示continue后面的代码跳过;代表只是中断本次循环

num = 1
while num < 10:
        num += 1
        if num == 3:
            continue
        print(num) 
        

16.while条件与else语句

while 条件:
    ...
else:#当所有的while都执行完后,再执行else;目前只有break可能会议让该else不执行
    ...
num = 1
while num < 10:
        num += 1
        if num == 3:
            continue
        print(num)
else:
    print('game over')

17.print不换行输出

print('hello world1.',end='')
print('hello world2.',end='')
print('hello world3.',end='')

print()等价于print(end='')

18.输入带星号的矩形

a = int(input('please input your hight:'))
b = int(input('please input your width:'))
j = 1
while j <= a:
    i = 1
    while i <= b:
        print ('*',end='')
        i += 1
    j +=1
    print()

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!