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

Python 基础---编码、数据类型

时间:2018-07-15 11:18:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ant   数字   ace   sans   统一   nic   comm   loop   编码方式   

 

Python2默认编码方式是ascii码

解决方式在文件首行:# -*-  coding:utf-8  -*-

Python3默认编码方式是UTF-8

 

8位一个字节

1Byte=8bit

 

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。

Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,

UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存.

Utf-8   一个中文3个字节表示

Gbk国内使用,一个中文用2个字节

 

变量:1.就是由数字、字母、下划线任意组成,不能以数字开头;

   2.不能是python中的关键字;

          3.要有可描述性,推荐使用下划线定义变量,如students_age,不要用拼音。

以下关键字不能声明为变量名:
[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

 

常量:指不变的量,一般使用大写,AGE_author = 18

注释:

单行:#

多行:’’’ ‘’’  或者 “”” “””

 

数据类型:

Type()查看数据类型

Print(100,type(100)) 数字类型

Print(‘100’,type(‘100’)) 字符类型

 

整数类型:int

int(整型)

在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647

在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807

 数字可以 +   -    *   /     **     %    //

字符串类型:str

在python中,加了双引号的都是字符串

age = 22          #int
age = 22#单引号
age = "22"       #双引号
age = ‘‘‘22‘‘‘    #3个引号
三个引号举例:
msg = ‘‘‘
今天我想写首小诗,
歌颂我的同桌,
你看他那乌黑的短发,
好像一只炸毛鸡。
‘‘‘
print(msg)

字符串相加,就是拼接,只能两个字符串拼接

字符串相乘,和数字相乘,打印多少次

 1 name = haha

 2 age = 18

 3 print(name+age)

 4 print(name*8)

 

布尔值:True 和False

用户交互:

 

Name = input(‘请输入你的名字’)

Age = input(‘请输入你的年龄’)

Print(name,age)

 

 

Input :等待用户输入

将你输入的内容赋值给了前面变量

Input出来的数据类型全是str

 

Name = input(‘请输入你的名字:’)

Age = input(‘请输入你的年龄:’)

Print(‘我的名字是’+name,’我的年龄是’+age+’’)

 

占位符

name = input(‘请输入姓名:‘)
age = input(‘请输入年龄:‘)
job = input(‘请输入工作:‘)
hobbie= input(‘你的爱好:‘)
info = ‘‘‘
------------ info of %s -----------
Name : %s
Age : %s
job : %s
Hobbie: %s
------------- end -----------------
‘‘‘ %(name,name,age,job,hobbie)
print(info)

# height = input(‘请输入身高:‘)
# msg = ‘我叫%s,今年%s,身高%s‘ %(name,age,height)
# print(msg)

 

and or not
#优先级,(),not, and,  or

# print(2 >1 and 1<4)
# print(2 >1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2)
# T or T or F
#T or F
#True

# 3>4 or 4<3 and 1==1 (F)
# 1 < 2 and 3 < 4 or 1>2 (T)
# 2 > 1 and 3 < 4 or 4 > 5 and 2 < 1(T)
# 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8(F)
# 1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6(F)
# not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6(F)
# print(2 or 1 < 3 )
# print(2 or 1 < 3 and 2)
# print(1 > 2 and 3 or 4 and 3 < 2)

 

 

in,not in :

 

判断子元素是否在原字符串(字典,列表,集合)中。

 

流程控制  if

if.....

if.....else

if....elif .....elif......else

 

流程控制  While

while 条件:   

    # 循环体
    # 如果条件为真,那么循环体则执行
    # 如果条件为假,那么循环体不执行终止循环
1.更改条件
2.break
3.continue
注意while...else
while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
count = 0
while count <=5:
count +=
1
if count == 3:
break
print(‘Loop‘,count)
else:
print(‘循环正常执行完啦‘)

# while True:
#     A=input(‘请输入数字‘)
#     if A ==‘7‘:
#         continue
#     print(A)

# count = 1
# while count<101:
#     print(count)
#     count+=2

# count = 1
# while count<101:
#     if count%2==1:
#         print(count)
#     count+=1

# sum = 0
# count =1
# while count <100:
#     if count%2==0:
#         sum=sum-count
#     else:
#         sum=sum-count
#     count+=1
# print(sum)
# print(‘a‘)
#
# i =0
# while i<3:
#     username = input(‘请输入账号:‘)
#     passwd = input(‘请输入密码:‘)
#     if username ==‘aaa‘ and passwd ==‘aaa‘:
#         print(‘success‘)
#         break
#     else:
#         print(‘error‘)
#     i=i+1

Python 基础---编码、数据类型

标签:ant   数字   ace   sans   统一   nic   comm   loop   编码方式   

原文地址:https://www.cnblogs.com/dzc18/p/9311886.html

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