1:条件判断语句:
age = 20
if age >= 18:
print('your age is', age)
print('adult')
#如果if
语句判断是True
,就把缩进的两行print语句执行了,否则,什么也不做。
age =
20
if age >= 18:
print('your age is', age)
print('adult')
else:
print('your age is', age)
print('teenager')
#如果if
判断是False
,不要执行if
的内容,去把else
执行了
age = 3 if age >= 18:
print('adult') elif age >= 6:
print('teenager') else:
print('kid')
#elif
是else if
的缩写,完全可以有多个elif
,所以if
语句的完整形式就是:
#if
语句执行有个特点,它是从上往下判断,如果在某个判断上是True
,把该判断对应的语句执行后,就忽略掉剩下的elif
和else
if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> else: <执行4>
输出函数:print()
输入函数:input()
>>> print('hello, world')
>>> hello Word
#在括号中加上字符串,就可以向屏幕上输出指定的文字,也可以接受多个字符串,用逗号“,”隔开,就可以连成一串输出。
#会依次打印每个字符串,遇到逗号“,”会输出一个空格,因此,输出的字符串是这样拼起来的。
>>>name = input( )
Jone
#可以让用户输入字符串,并存放到一个变量里。比如输入用户的名字:Jone
#打印变量名为name的代码
#要打印出name变量的内容,除了直接写name然后按回车外,还可以用print()函数
#input()输入函数:返回的数据类型是str
字符串
1.
>>>print(name)
>>>Jone
2.
>>>name
“Jone”
=====================================================
小程序:
>>>name = input()
Michael
>>>print('hello,', name)
Michael
hello, Michael
第一行代码会让用户输入任意字符作为自己的名字,然后存入name变量中;第二行代码会根据用户的名字向用户说hello,比如输入Michael
===================================================
修改后的小程序:
input()可以让你显示一个字符串来提示用户,于是我们把代码改成:
>>>name = input('please enter your name: ')
Michael
>>>print('hello,', name)
please enter your name: Michael
hello, Michael
输入输出统称为Input/Output,或者简写为IO