python初学

1.python_变量

name = '牛寒阳'
#.定义变量:字符串类型
age = 18
#.定义变量:整数型
money = 19.9
#.定义变量:小数类型
name_input = input('please input your name:')
age_input= input('please input your age:')
#.python2输入使用raw_input('---')
#.使用input接收输入的时候,获取到的都是字符串 

2.python_print

2.1print直接打印信息
print('hello world!')
print("hello world!")
print("let's go!")
print('他说“123”')
print('''let's go "wang"''')
#.print打印的字符串可以双引号也可以单引号
#.如果字符串中包含单引号则用双引号
#.如果字符串包含双引号则用单引号
#.如果字符串中既有单引号又有双引号则需要3对单引号
#.python2中输出不用括号,eg print helloworld
#.Python2中加入utf-8可输入汉字
#.关键字不能作为变量名 eg cmd or as......
2.2print通过调用变量打印信息
print(name)
#.变量输出使用print(变量名)
 2.3print通过字符串与调用变量打印信息
print('the name_input is :',name_input)
#.字符串和变量的集合输出使用拼接:print("字符串",变量名)
#.字符串和变量的集合输出也可使用占位符:print("字符串%s" %变量名) 注意两者区别,前者有逗号,后者无
print('hello,[%s]'%name_input)
print('hello,%s' %name_input)
print('hello,my name is %s,my age is %s' %(name_input,age_input))
print('hello,my name is %s,my age is %d,i have %.3f money' %('liuxia',18,22.1231456789))
#print('hello,my name is %s,my age is %d,' %name_input %age_input)
#age_input= int(age_input)
age_input = int(input('please input your age:'))
#.print('hello,my name is %s,my age is %d,' %name_input %age_input)---上面输出报错
#.字符串格式化:字符串中%s实现字符串的格式化,后面加变量名,eg print('字符串+占位符' %变量名)
#.字符串与变量名间有无空格均可
#.%s代表占位符,%s字符串,%d代表整数,%f代表小数,%.nf代表取小数的几位
say = input('what do you want to say:')
print(name+'say:'+say)
#.输出时可用+连接字符串
 
3.python_if
 
if判断逻辑:
if 条件:
****
elif 条件:
else:
****
 
score = float(input('your score is :'))
if score>=90:
print("your level is A")
elif score<90 and score>=80:
print("your level is B")
elif score<80 and score>=60:
print("your level is C")
elif score>=0 and score<60:
print("your level is D")
else:
print("please input your score,the score must be greater than zero")
 
if name_input!="liuxia":
print('it is student')
elif name_input=="niuhanyang":
print('it is teacher')
else:
print('the wrong name')
#.python中=不能表示判断,他表示赋值,我们需要用==判断
 
4.python_while
 
i = 0
while i<10:
print("while is in cycle:%d" %i)
i = i+1
if i == 2:
# print("while cycle is over,the reason is break")
# break
continue
print("while is in cycle:i is :",i)
else:
print('while cycle is over, and i = %d' %i)
#.循环分两种:while & for
#.如果用while循环,则必须有个计数器
#.break:立即结束循环
#.continue:结束本次循环,继续进行下一次循环
#.i=i+1 等同于 i+=1
 
5.python_random
 
随机数使用时先导入:import random
导入后就可给变量赋值:num = random.randint(num1,num2)
 
import random
num = random.randint(1,101)
while 1:
your_num = int(input('please inpt your num :'))
if your_num > num:
print('your_num is greater than num,please input a smaller num')
continue
elif your_num < num:
print('your num is too smaller,please input a greater num ')
continue
elif your_num < 0 and your_num > 100:
print('the num must in 0-100')
else:
print('wow,you are right!!!')
break
#随机产生num1至num2的数字:random.randint(num1,num2)
 
 
其他:
#.代码永远是从上往下执行的
#.注释整句python:ctrl+/,取消也是ctrl+/
#.快速复制一行代码:Ctrl+d
#.单行注释用#,多行注释可用3对单引号
#.使用input接收输入的时候,获取到的都是字符串 
#.python中=不能表示判断,他表示赋值,我们需要用==判断

 

posted on 2017-09-22 16:08  yezi_396  阅读(130)  评论(0编辑  收藏  举报

导航