py2
temp = "理解" # utf- 8
#解码, 需要指定原来的是什么编码
temp_unicode = temp.decode("utf-8")
#编码,需要指定要编写成什么编码
temp_gbk = temp_unicode.encode("gbk")
# windows终端需要的GBK
py3
#py3 自动装换 utf-8 unicode gbk
#py3 移除了python的unicode类型
temp = "理解"
#自动装换 utf-8 unicode gbk
temp_gbk = temp.encode("gbk")
0、python: pypy、cpython、jpython...
1、 ascii nuicode utf-8
2、2.7 # -*- coding: utf-8 -*-
3.x 默认utf-8
4、 变量名 = 值
变量名要求:
a . 数字 、字母 、下划线
b. 数字不能开头
c. 不能和py 关键字重复
5 、条件语句
if条件:、elif条件:、 else
6、 while循环
while条件:
从上到下执行一次
(判断条件是否为真)从上到下执行一次
(判断条件是否为真)从上到下执行一次
作业
用While 循环 把1 2 3 4 5 6 8 9 写出来
#!/usr/bin/env python
# -*- coding:utf-8 -*-
num = 1
while num < 10 :
if num == 7 :
num += 1
continue
if num == 10 :
break
num += 1
print(num)
OR
num = 0
while num < 10 :
if num != 7 :
print(num)
num += 1
分别用程序写出1到100的奇数和偶数
# num = 0
# while num < 100 :
# print(num)
# num += 2
# num = 1
# while num < 100 :
# print(num)
# num += 2
num = 1
sw= 0
while num < 100 :
sw = sum % 2
if sw == 1:
print (num)
num += 1
OR
num = 1
for num in range (1,100):
if num % 2 == 1:
print(num)
else:
continue
1+2+3+4+5......+100的和
# sum = 0
# num = 0
# while num < 101 :
# print (num)
# sum = sum + num
# num += 1
# print(sum)
1-2+3-4+5....+99求和
s = ""
sum = 0
start =1
while start <100:
if start % 2 == 1:
if start == 1:
s = str(start)
else:
s = s + "+" +str(start)
sum = sum+ start
else:
s = s + "-" + str(start)
sum = sum - start
start += 1
print (s)
print(sum)
OR
num =0
for i in range (1,100):
if i % 2 == 1:
num = num + i
else:
num = num - i
print(num)
用户有三次机会登录
n = 0
while n < 3 :
x = input("请输入用户名")
y = input("请输入密码")
if x == "123" and y == "123" :
print("yes")
break
else:
print("再输入一次")
n += 1
print ("x")
print ("y")