码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础(一)

时间:2017-01-01 23:54:35      阅读:427      评论:0      收藏:0      [点我收藏+]

标签:for循环   guess   特殊字符   开头   交互   think   python   字符编码   range   

一、python的版本

python版本分为2系列和3系列,但是目前大部分企业都使用2系列版本,但是3系列python版本已经成为趋势,因此本博客主要以3系列的python版本为主。

二、变量

变量即是预先定义的值,主要用于方便脚本编写,方便脚本调用。变量通常以数字、字母及下划线开头,例:1_pass 、_pass 、pass都可以作为变量使用,但是特殊字符不可以

例:

print("hello world")
name = "xiao yu"
name2 = name
print("My name is ",name,name2)
name = "lala"
print("My name is ",name,name2) ##后面这个name会显示lala,而name2会显示xiaoyu,因为name2指向的是内存中xiaoyu这个位置
name = "xiaoyu"
msg = ‘‘‘
name2 = name
print("My name is",name,name2)
‘‘‘
print(msg)

age = int(input("age:")) #integer
print(type(age) ,type(str(age)) )
job = input("job:")
salary = input("salary:")
info3 = ‘‘‘
-------- info of {0}---
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
‘‘‘ .format(name,age,job,salary)
print(info3)

三、字符编码与二进制

ASCII、unicode、utf-8、GB2312

四、用户交互

input命令:输入的所有字符都以字符串表示

例:name = input("name:") 

print(type(name))

str

五、if、else流程判断

条件判断:如果、否则。例:

_username = ‘xiaoyu‘
_password = ‘xiaoyu123‘
username = input("username:")
#password = getpass.getpass("password:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password")

print(username, password)

 

六、while循环

例:

Age_of_Dulant=50
count = 0
while True:
if count == 3:
break
Guess_age = int(input("ips age:"))
if Guess_age == Age_of_Dulant:
print("very smart!")
break
elif Guess_age > Age_of_Dulant:
print("think smaller!")
else:
print("think bigger")
count += 1
Age_of_Dulant=50
count = 0
while count < 3:
Guess_age = int(input("ips age:"))
if Guess_age == Age_of_Dulant:
print("very smart!")
break
elif Guess_age > Age_of_Dulant:
print("think smaller!")
else:
print("think bigger")
count += 1
if count == 3:
continue_confirm = input("do you want to keep guessing:")
if continue_confirm != ‘n‘:
count = 0

七、for循环

例:

#Age_of_Dulant=50
#for i in range(3):
# Guess_age = int(input("ips age:"))
# if Guess_age == Age_of_Dulant:
# print("very smart!")
# break
# elif Guess_age > Age_of_Dulant:
# print("think smaller!")
# else:
# print("think bigger")
#else: ##表示若上面for循环完成,则执行此else,否则,不执行
# print("you have try too many times,fuck off!")
#for i in range(10):
# if i < 4:
# print("loop:",i)
# else:
# continue
# print("hehe...")

for i in range(10):
print("_________:",i)
for j in range(10):
print(j)
if j > 5:
break

Python基础(一)

标签:for循环   guess   特殊字符   开头   交互   think   python   字符编码   range   

原文地址:http://www.cnblogs.com/xiaoyu866/p/6241726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!