python 基础
注意:在一般编程过程中,大写代表常量,而小写则代表变量
x = 2y = 3z = x x =5结果:x = 5,z = 2
Python中的数据类型
网址:http://www.yiibai.com/python/python_basic_operators.html
Python中使用的是Unicode字符,在Python中ord()可以查看ascall值。
在存储字符的过程中,默认是使用ascall进行存储字符,如果这样写就是使用Unicode进行存储。
>>> a = u'lala'>>> type(a)<type 'unicode'>
name =”飞天”
print name
飞天
name
'\xe9\xa3\x9e\xe5\xa4\xa9’
#这是utf-8(ascall是utf-8的一个子集),他灵活存储汉字和字母,当你是汉字的时候,使用3个字节,如果是英文就用一个字节。\xe9\xa3\x9e这就是飞字。存储在内存的过程是使用Unicode,因为不能同时使用两种字符集。在硬盘中存储是使用硬盘中,就是使用utf-8,因为能节省空间Unicode不管是汉字还是字母都是使用2个字节。
在Unicode转化成utf-8中间需要一个转换过程,转换过程就如下
name = u“飞天”name.encode(‘utf-8’) #转化成utf-8#解开b =name.encode(‘utf-8’) #这是一个将他转化成字符串类型的name.decode(‘utf-8’) #重新转化成Unicode
支持中文,在代码首页加入:* coding:utf-8 *#有四种方式,详见博客。
导入模块有好几种方式:
import sys这样调用的话,就是要加上你模块的名称例如:sys.argv.from sys import argv
这样就直接 argv进行调用import multiprocessing as multi
将模块取一个别名,用来代替繁琐的模块名称form sys import * #不建议使用
把这个模块所有的方法都导进来,也是直接使用 流程控制if … elif …else #永远只有一个结果for i in range(1,100).
while …else
练习#猜年龄name = raw_input("Please input your name:")info = 'This is var will be print out'age = input("Age:")job = raw_input("Job:")real_age = 29for i in range(1,10):if age > 29: print "think smaller!"elif age == 29: print '\033[32;1mGOOD! 10RMB!\033[0m'else: print "think bigger"print"you still get %s shots" %(10-i)age = input("Age:")lala = """Name:%sAge:%sJob:%s"""%(name,age,job)print "-----------------"
#输出你想打印的数值count = 0print_num = input("which loop do you want to be print out ?")while count < 10000:if count == print_num: choice = raw_input("Do you want to continue the Loop?(y/n)") if choice == "n": break else :#这里使用一个while循环也可以。#while print_num <= count# print_num = input("which loop do you want to be print out ?")# print print "%d已经过了,sx" %(count) print_num = input("which loop do you want to be print out ?") if print_num < count: print "%d已经过了,sx" %( print_num) print_num = countelse : count +=1 print 'Loop',countelse:print "Loop",count
作业编写登录接口输入密码和用户名认证成功后显示欢迎信息认错三次后锁定
#代码 import sys retry_limit = 3 retry_count = 0 account_file = "accounts.txt" lock_file = "account_lock.txt" while retry_count < retry_limit: #只要不超过3次及不断循环 username = raw_input("\033[32;1mUsername:\033[0m") lock_check = file(lock_file,) #当用户输入用户名后,打开LOCK文件以检查此用户已经LOCK for line in lock_check.readlines(): line = line.split() if username == line[0]: sys.exit('033[31!1mUsername %s os locked!\033[0m' %username) password = raw_input('\033[32;1mPassword:\033[0m') f = file (account_file,'rb') match_flag =False for line in f.readlines(): user,passwd = line.strip('\n').split() #去掉每行多余的,并把这一行按空格分成两列,分别赋值user,passwd两个变量 if username == user and passwd == password: print "Match!",username match_flag = True break f.close() if match_flag == False: print "usrname" retry_count +=1 else : print "welcome log the system!" else: print "your account is locked!"
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。