Python是一门面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。
解释型:在运行的时候将程序翻译成机器语言,所以运行速度相对于编译型语言要慢。如:Python/JavaScript / Perl /Shell/C#/java
编译型:先编译好,执行的时候不用再一行一行编译了。如:C/C++
一、Python数据类型
1.字符串
2.布尔类型
3.整数
4.浮点数
5.数字
6.列表
7.元组
8.字典
9.日期
1、字符串
#coding=utf-8
# print('hello,world!')
# name = "'你好'"
# name1 = '"你好"'
# name2 = ''''你好',,"呵呵"'''
# print(name,name1,name2)
#转义字符\可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\,Python还允许用r''表示''内部的字符串默认不转义
# print('I\'m ok,')
# print('I\'m learning\npython.')
# print('\\\n\\')
# print('\\\t\\')
# print(r'\\\t\\')
# print('''line1
# line2
# line3''')
2、布尔值:布尔值可以用and、or和not运算
# n = 123
# f = 456.789
# s1 = 'hellow,world'
# s2 = 'hellow,\'admin\''
# s3 = r'hello,"bart"'
# s4 = r'''hello,
# lisa!'''
# print(n,f,s1,s2,s3,s4)
# bool = False
3、整数
# int = 20
# print(int)
4、浮点型
# float = 2.2222
# print(float)
5、数字:包括整数、浮点数
# 5.1、删除数字对象引用
# a = 1
# b = 2
# c = 3
# del a #删除了a 后,print(a)会报错
# print(b)
# 5.2数字类型转换
# int(-11.99) --返回 -11
# float(10) ---返回 10.0
# str(111) ---返回 '111'
# repr(111) ---返回 '111'
# 5.3数学函数
# abs(-11) ---返回11
6、列表
# 6.1初始化列表
# list = ['张三','李四','王五']
# nums = [2,5,8,1,12,15,7]
# print(list,nums)
# 6.2访问列表的值
# '''nums[0]:1'''
# print("nums[0]:",nums[0])
# print("nums[2:5]",nums[2:5])
# print(nums[:-1]) #从最开始的元素切割到最后一个(不包括最后一个)
# print(nums[1:]) #从第二个元素切割到最后一个
# print(nums[1]) #返回第二个参数
# print(nums[1:3]) #从第二个元素切割到第四个(不包括第四个)
# 6.3操作列表
# nums[0] ='001'
# print(nums) #新列表:['001', 5, 8, 1, 12, 15, 7]
# del nums[0]
# print(nums) #新列表:[5, 8, 1, 12, 15, 7]
# 6.4列表脚本操作符
# print(len([1,2,3])) #3
# print([1,2,3] + [4,5,6]) #[1, 2, 3, 4, 5, 6]
# print(['hi' * 4]) #['hihihihi']
# print(3 in [1,2,3]) #True
# for x in [1,2,3]:
# print(x) #1 2 3
# 6.5列表截取
# L =['spam','Spam','SPAM']
# print(L[2]) #SPAM
# print(L[-2]) #Spam
# print(L[1:]) #['Spam', 'SPAM']
# print(L[:1]) #['spam']
# 6.6列表函数、方法
# aa = [1,1,2]
# list.append(aa,3)
# print(aa) #[1, 2, 3]
7、元组(元素值不允许修改)
# tup1 = ('physics','chemistry',1990,200)
# tup2 = (1,2,3,4,5)
# print(tup1[0]) #physics
# print(tup1[0:2]) #('physics', 'chemistry')
# tup3 = tup1+tup2
# print(tup3) #('physics', 'chemistry', 1990, 200, 1, 2, 3, 4, 5)
# del tup3 #删除元组
# 7.1元组运算符
# print((1,2) + (3,4)) #(1, 2, 3, 4)
# print(['*'] * 8) #['*', '*', '*', '*', '*', '*', '*', '*']
# print(3 in (1,2,3)) #True
# for x in (1,2,3):
# print(x) #1 2 3
# a = (1,2,2)
# b = (1,2,3)
# print(a[0]) #1
# print(a[1:]) #(2,2)
# print(a[-1]) #2
# print(min(a)) #1:返回元组中最小的
# print(max(a)) #2:返回元组中最大的
# print(len(a)) #3:计算元组个数
8、字典
# dict = {'name':'zara','age':'18','class':'first'}
# print(dict["age"]) #18
# dict["age"]=90
# print(dict["age"]) #90
# dict["school"]="qinghua" #增加键值
# print(dict) #{'school': 'qinghua', 'name': 'zara', 'age': 90, 'class': 'first'}
# del dict['name'] #{'class': 'first', 'school': 'qinghua', 'age': 90}
# print(dict)
# del dict #删除dict字典
# print(dict)
# dict1 = {'name':'zhangsan','age':'18'}
# dict2 = {'name':'lisi','age':'18'}
# print(len(dict1)) #2
# print(str(dict1)) #{'age': '18', 'name': 'zhangsan'}
9、日期和时间
# import time
# import datetime
# localtime = time.localtime(time.time())
# print(localtime) #time.struct_time(tm_year=2017, tm_mon=5, tm_mday=19, tm_hour=9, tm_min=39, tm_sec=2, tm_wday=4, tm_yday=139, tm_isdst=0)
#日期转为字符串
# print(time.strftime('%Y-%m-%d %H:%M:%S'))#2017-05-19 09:39:02
# print(datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')) #2017-05-19 09:40:52
# print(str(datetime.datetime.now()))[:19] #2017-05-19 09:41:29.337263
#获取时间差
# oneday = datetime.timedelta(days=1)
# print(oneday)
# today = datetime.date.today() #今天2017-05-19
# print(today)
# print(datetime.date.today() - oneday) #昨天2017-05-18
# print(datetime.date.today() + oneday) #明天2017-05-20
# print(datetime.datetime.strftime(today,'%Y-%m-%d %H:%M:%S')) #获取每天零点2017-05-19 00:00:00
#
# print(datetime.timedelta(milliseconds=1)) #1毫秒0:00:00.001000
# print(datetime.timedelta(seconds=1)) #1秒0:00:01
# print(datetime.timedelta(minutes=1)) #1分钟0:01:00
# print(datetime.timedelta(hours=1)) #1小时1:00:00
# print(datetime.timedelta(days=1)) #1天1 day, 0:00:00
# print(datetime.timedelta(weeks=1)) #1星期7 days, 0:00:00
二、条件、循环语句
# break:跳出最近所在的循环(跳过整个循环语句)
# continue:跳到最近所在循环的开头处(来到循环的首行)
1、for循环
# for i in range(10):
# print(i,"Python is my friend")
2、while循环
# cunt = 0
# while cunt < 10:
# print(cunt,"hello")
# cunt += 1
3、if条件语句
# sum =int(input("enter a number:"))
# if sum >= 90: #循环条件
# print("优秀") #操作执行
# elif sum >= 80:
# print("良好")
# elif sum >= 60:
# print("及格")
# else:
# print("需要提升了")
#在Python中,数字非零(0) 代表:真(True);对象非空(None) 代表:真(True)
# print(True==0) #false
# print(True!=0) #true
# print(False==0) #true
# print(False!=0) #false
# i =input("enter a string:") #有值输出right,空值输出error
# if i:
# print("right")
# else:
# print("error")