第一章python入门
一、第一个程序
1、helloWorld
print("hello world")
2、最简单的可执行的python文件
linux命令行下:
vim hello.py
hello.py文件中插入如下代码:
#!/use/bin/env python
print("hello,World")
执行:./hello.py 即可。
ps:执行前要给予hello.py执行权限,chmod 755 hello.py
3、python格式化输出
输入信息
name = input('username:')
age = int(input("age:"))#integer
job = input("job:")
print(type(age))
第一种(内存消耗大不建议使用)
info1 = ''' -------- info of '''+name+''' --- name:'''+name+''' Age:'''+age+''' job:'''+job+''' '''
print(info1)
第二种
info2 = ''' -------- info of %s --- name:%s Age:%d job:%s '''%(name,name,age,job)
print(info2)
第三种
info3 = '''
-------- info of {_name} ---
name:{_name}
Age:{_age}
job:{_job}
'''.format(_name=name,_age=age,_job=job)
print(info3)
第四种
info4 = '''
-------- info of {0} ---
name:{0}
Age:{1}
job:{2}
'''.format(name,age,job)
print(info4)
4、条件语句
age_of_oldboy =56
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("you got it.")
break
elif guess_age >age_of_oldboy :
print("think smaller")
else :
print("think biger")
5、循环语句
while循环
age_of_oldboy =56
count =0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy :
print("you got it.")
break
elif guess_age >age_of_oldboy :
print("think smaller")
else :
print("think biger")
count +=1
if count==3 :
continue_confirm=input('do you want to continue')
if continue_confirm != 'n':
count=0
else :
print('funck off you have tried too many times')
for循环
for i in range(0,10,2):
print("loop ",i)
浙公网安备 33010602011771号