yezuocheng

导航

 
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

print("Hello, World")
#print 中“,”代表空格
print('Hello','World')
print('Hello''World')
#转义字符\
#\n表示换行,\t表示制表符
print('\\')
print('\"')
print('I\'m \"Ok\"!')
print('\\\n\\')
#r''
print(r'\\\n\t\\')
print('\\\n\t\\')
#'''...'''
print('''line1
line2
line3'''
      )
print(r'''\
      \
      \n\t''')
#输入 input()
a=input()
print(a)
#输入提示input(。。。)
a=input('please input a number')
print(a)
#if else
a=100
if a>=0:
    print (a)
else:
    print (-a)
#数据类型
a=123
type(a)
#转换数据类型int()
a=int(a)
#布尔值
True
False
3>2
3>5
#and 
True and True
True and False
False and False
#or 
True or True
True or False
False or False
#not
not True
not False
#空值
None
#python是动态语言
a=1
a="123"
print(a)
#赋值
a = 'ABC'
b = a
a = 'XYZ'
print(b)
#除法
10/3
#地板除法
10//3
#mod%
10%3
#Python 3用unicode编码
print('有中文的str')
#获取字符的整数ord()
ord('A')
#把编码转换为对应字符chr()
chr(65)
chr(ord('A'))
'\u4e2d\u6587'
#str变为字节传输
x=b'ABC'
#str通过encode()方法可以编码为指定的bytes
'ABC'.encode('ascii')
'中文'.encode('utf-8')
'中文'.encode('ascii')
#要把bytes变为str,就需要用decode()
b'ABC'.decode('ascii')
b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
b'\xe4\xb8\xad\xff'.decode('utf-8')
b'\xe4\xb8\xad\xff'.decode('utf-8',errors='ignore')
#str中字符数len()
len('ABC')
#如果换成bytes,len()函数就计算字节数
len(b'ABC')
len(b'\xe4\xb8\xad\xe6\x96\x87')
len('中文'.encode('utf-8'))
#格式化方法
#%d    整数
#%f    浮点数
#%s    字符串
#%x    十六进制整数
'Hello,%s'% 'world'
'Hi, %s, you have $%d'%('Michael',100000)
#只有一个占位符,()可以省略
'Hello,%s'% 'world'
'Hello,%s'% ('world')
#保留2位小数
'%.2f'%(3.1415926)
#补0
'%d,%02d'%(3,4)
'Age=%s. Gender=%s'%(25,'F')
'growth rate = %s %%'%3
#format()方法
'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明',178.54)
#number(数字)、string(字符串)、Boolean(布尔值)、None(空值)
#list(列表)、tuple(元组)、dict(字典)、set(集合)。
#list 可以随意添加删除
classmates=['Bob','Michael','Tracy']
len(classmates)
#索引,第一个元素为0,最后一个元素为-1,len()-1
classmates[0]
classmates[len(classmate)-1]
classmates[-1]
#在最后增加一个元素.append()
classmates.append('Adam')
#插入.insert()
classmates.insert(1,'Jack')
#删除.pop()默认删除最后一个或者输入位置
classmates.pop()
classmates.pop(1)
#替换[]
classmates[1]='Jason'
#list内数据类型可以不同
L=[123,'ABC',True]
#list包含list
s=[[1,2,3],'ABC',123]
len(s)
s[0]
#二维数组
s[0][1]
#空list[]
[]
len([])
#tuple一旦赋值不能修改指向
t=(1,2)
#空tuple
t1=()
#单一tuple
t2=(1,)
#tuple中含list,list可以修改
t3=(1,2,[1,2,3])
t3[2][1]=1

 

posted on 2019-11-13 15:41  yezuocheng  阅读(138)  评论(0)    收藏  举报