这个是最简单的输出 hello world ,话说有个笑话是以为退休的程序员百无聊赖想着得学点什么,最后选择了书法,拿起毛笔,举笔良久不知该写什么,最后毅然落下“hello world”2333
print 语法在不同的 python 版本里会有些许偏差,还有一种是不带括号的写法
#print("hello world!")
注释语法
单行注释时在句首加一个 # 号即可
多行注释时,在行首和行尾分别添加 ''' 即可
另一个必用的简单函数,初学者一定会用到的等待用户输入。
#等待用户输入内容函数: input, 3.x 以后 Input 等效于之前版本的 raw_input
#input_name = input("enter your name:")
#print(input_name)
#python 核心编程第二章习题 2-7
#while 循环
'''
string1 = input("enter a string please:")
i = len(string1) # 或者用户输入的 string 长度, len 是自带函数
j = 0
while j < i: # while 循环的语法跟其他语言类似,不往细了说咯
print(string1[j])
j += 1
#for 循环
string1 = input("enter a string please:")
for item in string1: # 这个是 for 循环跟其他语言不太一样,需要理解一下,这里相当于把每个字母当成一个 item 去输出
print(item)
'''
#while 循环实现 2-8
'''
atuple = (1,2,3,4,5)
tulength = len(atuple)
i = amount = 0
while i<tulength:
amount += atuple[i]
i+=1
print(amount)
i = 0
amount = 0
list_for_user = []
while i<5:
j = int(input("enter a number:"))
list_for_user.append(j)
amount += list_for_user[i]
i += 1
print(list_for_user)
print(amount)
'''
#for 循环实现2-8
'''
alist = [] #创建一个空的列表
i = 0
amount = 0
for i in range(5):
j = int(input("enter a number:"))
alist.append(j) #由于列表是空的,所以不能使用[1]这种序号来查询,只能用 append 函数在列表尾部添加元素
amount += alist[i]
print(alist)
print(amount)
'''
#2-9
atuple = (1,2,3,5,7)
i=0
len_of_atuple = len(atuple)
for item in atuple:
i += item
print(i/len_of_atuple)
#2-10
'''
num = int(input("enter a number between 1 to 100:")) #我这个用户默认输入的是 string 不进行 int 转换的话会报错
i = 1
while i != 0:
if 1 <= num <= 100:
print("succeed")
i = 0;
else:
num = int(input("you shoule enter a num between 1 to 100:"))
'''
#2-11
'''
print("1 means get amount of five nums, 2 means get average of five nums, X means ending ")
flag = 1
amount = 0
condition = "c"
while condition != "X":
while condition != "X":
while flag == 1:
j = input("enter your choose:")
if j == "1" or j == "2" or j == "X":
flag = 2
else:
print("selece your choice from 1,2,X")
if j == "1":
for i in range(5):
amount += int(input("enter a num:"))
print(amount)
flag = 1
elif j == "2":
for i in range(5):
amount += int(input("enter a num:"))
print(amount / 5)
flag = 1
else:
condition = "X"
'''
#2-12
#print(dir.__doc__)