首页 > 代码库 > Python day1

Python day1

一、变量
#!/usr/bin/env python
# -*- utf-8 -*-
# Author:Wang Lulu

name ="Wang Lulu"  #变量赋值
name2 = name   #变量赋值可以使用变量(这里name2是指向Wanglulu的并不随着name变量的变化而变化)
print("Hello world")  #输出格式
print("My name is ",name,name2)  #输出格式

name = "Fan Shuangshuang"
print(name,name2)
GF_of_Wanglulu = "Fan Shuangshuang"   变量定义规则(1一般以下划线区分2可以下划线开头但是最多写两个3或者利用字母开头大写区分)
GFOfWanglulu = "Fan Shuangshuang"
print("I Love You",GF_of_Wanglulu)
二、字符编码
1  2  4  8  16  32  64  128  256

1  1  1  1  1  1  1  1  1

2**0  2**1  2**2  2**3  2**4  2**5  2**6  2**7  2**8
2=1+1
4=2+1+1
8=4+2+1+1
16=8+4+2+1+1

ASCII 最多只能用8位来表示(一个字节),即2**8=256-1,所以ASCLL只能最多表示255个符号
127个西方的字符(包括字母数字特殊符号)预留了128-256之间的给其他国家用
GB2312     1980年   7445个字符
GBK1.0     1995年   21886个符号
GB18030    2000年   27484个汉字


Unicode(万国编码,单一码) 统一2bytes
      UTF-8  英文:1bytes  中文:3bytes
python3比python2相比3支持中文,默认使用的是utf-8字符集

三、用户交互程序
1、python的注释
单行注释 #
多行注释  ‘‘‘开始‘‘‘结束
‘‘‘还有一个用途打印多行
例:msg = ‘‘‘
name ="Wang Lulu"
name2 = name
print("Hello world")
print("My name is ",name,name2)
‘‘‘
print(msg)

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Wang Lulu
‘‘‘
username = input("username:")
password = input("password")
raw_input  2.x   input 3.x
input  2.x (接受的是变量,输入的是变量,找不到就会报错必须先定义一个变量)
print(username,password)
‘‘‘
name = input("Name:")
age = int(input("Age:"))  #interger 整型  字符串转换整型
print(type(age) ,type( str(age) ))   #str(age) 整型转换字符串
job = input("Job:")
salary = int(input("Salary:"))
格式化输出
%d  data 数字
%s  string 字符
%f  浮点类型,小数一般不常用
1、info = ‘‘‘
-------------info of ‘‘‘ +name+‘‘‘-----------
Name:‘‘‘+name+‘‘‘
Age:‘‘‘+age+‘‘‘
job:‘‘‘+job+‘‘‘
Salary:‘‘‘+salary
‘‘‘
print(info)

2、
name = input("Name:")
age = input("Age:")
print(type(age) ,type( str(age) ))
job = input("Job:")
salary = int(input("Salary:"))
info =‘‘‘
--------------info of %s--------------
Name:%s   #string 字符串
Age:%s
job:%s
Salary:%s
‘‘‘%(name,name,age,job,salary)
print(info)

3、
name = input("Name:")
age = int(input("Age:"))  #interger 整型  字符串转换整型
print(type(age) ,type( str(age) ))
job = input("Job:")
salary = int(input("Salary:"))
info =‘‘‘
--------------info of %s--------------
Name:%s   #string 字符串
Age:%d  #只等接收数字字符类型为数字  %f  浮点,指小数
job:%s
Salary:%d
‘‘‘%(name,name,age,job,salary)
print(info)


4、
name = input("Name:")
age = int(input("Age:"))  #interger 整型  字符串转换整型
print(type(age) ,type( str(age) ))
job = input("Job:")
salary = int(input("Salary:"))
info2 =‘‘‘
--------------info of {_name}--------------
Name:{_name} 
Age:{_age}
job:{_job}
Salary:{_salary}
‘‘‘.format(_name=name,
           _age=age,
           _job=job,
           _salary=salary)
print(info2)

5、name = input("Name:")
age = int(input("Age:"))  #interger 整型  字符串转换整型
print(type(age) ,type( str(age) ))  #str(age) 整型转换字符串
  job = input("Job:")
salary = int(input("Salary:"))
info3 =‘‘‘
--------------info of {0}--------------
Name:{0} 
Age:{1}
job:{2}
Salary:{3}
‘‘‘.format(name,age,job,salary)
print(info3)
四、if、else判断语法

1、#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Wang Lulu

import getpass
_username = "WangLulu"
_password = "oracle123"
username = input("Pleace input your username:")
#password = getpass.getpass("Pleace input your password:")获取密码不显示
password = input("Pleace input your password:")
print(username,password)

if _username == username and _password == password:
    print("Welocome user {name} login...".format(name=username))
else :
    print("Invalid username or password!")
2、#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Wang Lulu

age_of_FanShuangshuang = 13

guess_age = int(input("Guess age:"))

if guess_age == age_of_FanShuangshuang:
    print("Yes,you got it.")
elif guess_age > age_of_FanShuangshuang:
    print ("Think amaller....")
else:
    print("Think bigger!")
五、while循环
1、#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Wang Lulu

count = 0
while True:
    print("count:",count)
    count = count +1 #count +=1

2、#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Wang Lulu


age_of_FanShuangshuang = 13
count = 0
while True:          ------------------------->>>                                            等价于 while count<3
    if count ==3:
        break
    guess_age = int(input("Guess age:"))
    if guess_age == age_of_FanShuangshuang:
       print("Yes,you got it.")
       break
    elif guess_age > age_of_FanShuangshuang:
       print ("Think amaller....")
    else:
       print("Think bigger!")
    count +=1


人性化提示
age_of_FanShuangshuang = 13
count = 0
while True:
    if count ==3:
        break
    guess_age = int(input("Guess age:"))
    if guess_age == age_of_FanShuangshuang:
       print("Yes,you got it.")
       break
    elif guess_age > age_of_FanShuangshuang:
       print ("Think amaller....")
    else:
       print("Think bigger!")
    count +=1
if count ==3: #等价于else:
    print("You have tried too many times...fuck off")

 

Python day1