python基础学习(一)

  1. 入门教程
    https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 廖雪峰的教程
    2、python基础
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
print("hello python")
print("100+200","=",100+200)
#name = input()
#print("senbie",name)
a = 100
if a>=0:
    print(a)
else:
    print(-a)
print(r'\\\t\\\\') #r''  ''中的字符串不转意
#'''...''' 换行
print('''line1   
line2
line3''')
print(r'''hello,\n
world''')
print(3>2)
print(3<2)
print(True and True)
print(True and False)
print(True or False)
print(True or True)
print(not True)
#空值 none
a = 123 
print(a)
a = "ABC"
print(a)
print(10/3)
print(9/3)
print(10//3)
print(10%3)
#在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8
#python 3 ,字符串以Unicode编码
print(ord('A')) #字符的整数
print(ord('中'))
print(chr(66)) #将编码转换成字符
print(chr(25991))
x = b'ABC' #bytes 字节类型 b标志 3字节
print(x)
print('ABC'.encode('ascii')) #Unicode -> assii(bytes)
print("中文".encode('utf-8'))
#"中文".encode('ascii')  异常    多字节 -> 单字节 编码
print(len("ABC"))  #包含的字符数  3 
print(len("中文")) #包含的字符数  2
print(len(b'ABC')) #包含的字节数  3
print(len('中文'.encode('utf-8'))) #包含的字节数  6

#格式化字符串
print('%2d-%02d' % (3,1))  #占位  0补齐
print('%.2f' % 3.1415926)  #小数点后2位
print('Age: %s. Gender: %s' %(25,True)) #牛叉的%s 所有的都格式化位字符串
print('growth rate: %d %%' % 7) #%%用来表示一个%
formates = 'Hello,{0},成绩提升了 {1:.1f}%'.format('小明',17.25)
print(formates)

#List和tuple
classmate = ['Alice','Bob','Tom']  #一个List
print(classmate)
print(len(classmate))
print(classmate[0])
#print(classmate[4]) #异常
print(classmate[-1]) #获取最后一个元素 倒数第一个
print(classmate[-2]) #倒数第二个
classmate.append('Adam')
print(classmate)
classmate.insert(1,'jack')
print(classmate)
classmate.pop()
print(classmate)
classmate.pop(1)
print(classmate)
classmate[1]='Sarah'
print(classmate)
s = ['python','java',['asp','php'],'scheme']
print(s[2][1])

#tuple一旦初始化就不能修改
classmate = ('michael','Bob','Tracy')   #tuple
t = (1,) #定义一个元素的tuple
t = ('a','b',['A','B'])
t[2][0] = 'X'
t[2][1] = 'Y'
print(t)  # tuple中的list可以修改

#条件判断,python的缩进规则
age = 5
if age >=18:
    print('your age is',age)  
    print('adult')
elif age>=6:
    print('kid')
else:
    print('your age is',age)
    print('teenager')

#s = input('birth')
s = '1982'
birth = int(s)
if birth<2000:
    print('00前')
else:
    print('00后')

#循环
namse = ['Alice','Bob','Tracy']
for name in namse:
    print(name)


sum = 0
for x in [1,2,3,4,5,6,7,8,9,10]:
    sum = sum + x
print(sum)

s = list(range(101)) #转换为list
print(s)

sum = 0
for x in range(101):
    sum = sum + x
print(sum)

sum = 0
n = 99
while n>0:
    sum = sum + n
    n = n - 2
print(sum)

n = 1
while n <= 100:
    if n > 10:
        break   # break   continue
    print(n)
    n = n+1
print('END')

#dict(map)  and set
#dict(map)

d = {'Michael': 95,'Bob':75, 'Tracy':85}
print('Michael score',d['Michael'])
d['Adam'] = 67
print('Adam score',d['Adam'])
print('THomas' in d)  #判断key是否存在
print(d.get('ssss'))  #不存在返回none
d.pop('Bob') #删除一个 key values

#set
s = set([1,2,3])  #传入一个 list [1,2,3]
print(s)
s = set([1,1,2,2,3,3])
print(s)
s.add(4)
print(s)
s.remove(4)
print(s)

你可能感兴趣的:(python学习系列)