输入输出
python使用input()函数输入字符串,print()函数输出字符串。
print("Hello world") # 输出字符串
print(8) # 输出数字
name = input()#输入名字,输出,hello+名字
print('Hello', name)
输入函数我们使用input(),这个函数默认为字符串类型
print('please input you choice')
a = input()
if(a == '0'):
print("a is 0")
if(a == "1"):
print("a is 1")
注释
用#
实现单行注释,'''
或''' '''
实现多行注释
#注释1(只能单行)
'''
注释2
注释3
注释4...
注释n
'''
"""
注释n+1
注释n+2
注释....
"""
各种数据类型
大概跟C/C++中差不多,但个人认为更简洁、分类更小一点
整数、浮点数、字符串(用单引号、双引号、三引号括起来都可以),布尔(C/C++中的bool),不可变的常量和可变的变量
运算符总结
还有几个我没见过的哈哈,重点记一下
几条基本语句
- if语句
"if语句的使用"
" if 判断条件1:(冒号是必须的)"
"' 执行语句1...(注意缩进,执行语句不可以跟if同齐)"
- while语句
sum = 0
average = 0
index = 5
while index > 0:
sum += index
index -= 1
average = sum/5
print("sum=", sum)
print("average=", average)
2. for语句
str = 'I am lmy'
for s in str:
print(s)
3. break语句
index = 0
temp = 1
while (1):
index += temp
temp = temp+1
if(temp > 10):
break
print(index)
- continue语句
temp = 1
for temp in range(5):
if(temp == 2):
continue
print(temp)
- pass语句
一条几乎没什么用的语句,仅仅用来占位置,保持语句的完整性
temp = 1
for temp in range(5):
if(temp == 2):
pass
print(temp)
对比continue语句,你就会发现结果的不同
相关基本语句: