Python3.x:基础学习

Python3.x:基础学习

1,Python有五种标准数据类型

  • 1.数字
  • 2.字符串
  • 3.列表
  • 4.元组
  • 5.字典

 (1).数字

  数字数据类型存储数字值。当为其分配值时,将创建数字对象。

var1 = 10
var2 = 20

 可以使用del语句删除对数字对象的引用。 del语句的语法是 

del var1[,var2[,var3[....,varN]]]]

 可以使用del语句删除单个对象或多个对象。

del var
del var_a, var_b

 Python支持三种不同的数值类型 -

  • int(有符号整数)
  • float(浮点实值)
  • complex(复数)

 Python3中的所有整数都表示为长整数。 因此,长整数没有单独的数字类型。

 (2).Python字符串

 Python中的字符串被标识为在引号中表示的连续字符集。Python允许双引号或双引号。 可以使用片段运算符([][:])来获取字符串的子集(子字符串),其索引从字符串开始处的索引0开始,并且以-1表示字符串中的最后一个字符。

 加号(+)是字符串连接运算符,星号(*)是重复运算符。例如:

str = 'bolg.com'

print ('str = ', str)          # Prints complete string
print ('str[0] = ',str[0])       # Prints first character of the string
print ('str[2:5] = ',str[2:5])     # Prints characters starting from 3rd to 5th
print ('str[2:] = ',str[2:])      # Prints string starting from 3rd character
print ('str[-1] = ',str[-1])      # 最后一个字符,结果为:'!'
print ('str * 2 = ',str * 2)      # Prints string two times
print ('str + "TEST" = ',str + "TEST") # Prints concatenated string

 执行结果:

str =  blog.com
str[0] =  b
str[2:5] =  og.
str[2:] =  og.com
str[-1] =  m
str * 2 =  blog.comblog.com
str + "TEST" =  blog.comTEST

 (3).Python列表

  列表是Python复合数据类型中最多功能的。 一个列表包含用逗号分隔并括在方括号([])中的项目。在某种程度上,列表类似于C语言中的数组。它们之间的区别之一是Python列表的所有项可以是不同的数据类型,而C语言中的数组只能是同种类型。

  存储在列表中的值可以使用切片运算符([][])来访问,索引从列表开头的0开始,并且以-1表示列表中的最后一个项目。 加号(+)是列表连接运算符,星号(*)是重复运算符。例如:

list = [ 'yes', 'no', 786 , 2.23, 'minsu', 70.2 ]
tinylist = [100, 'maxsu']

print ('list = ', list)          # Prints complete list
print ('list[0] = ',list[0])       # Prints first element of the list
print ('list[1:3] = ',list[1:3])     # Prints elements starting from 2nd till 3rd 
print ('list[2:] = ',list[2:])      # Prints elements starting from 3rd element
print ('list[-3:-1] = ',list[-3:-1])    
print ('tinylist * 2 = ',tinylist * 2)  # Prints list two times
print ('list + tinylist = ', list + tinylist) # Prints concatenated lists

 结果:

list =  ['yes', 'no', 786, 2.23, 'minsu', 70.2]
list[0] =  yes
list[1:3] =  ['no', 786]
list[2:] =  [786, 2.23, 'minsu', 70.2]
list[-3:-1] =  [2.23, 'minsu']
tinylist * 2 =  [100, 'maxsu', 100, 'maxsu']
list + tinylist =  ['yes', 'no', 786, 2.23, 'minsu', 70.2, 100, 'maxsu']

 (4).Python元组

 元组是与列表非常类似的另一个序列数据类型。元组是由多个值以逗号分隔。然而,与列表不同,元组被括在小括号内(())。

 列表和元组之间的主要区别是 - 列表括在括号([])中,列表中的元素和大小可以更改,而元组括在括号(())中,无法更新。

 元组可以被认为是只读列表。 例如:

tuple = ( 'maxsu', 786 , 2.23, 'yiibai', 70.2  )
tinytuple = (999.0, 'maxsu')

# tuple[1] = 'new item value' 不能这样赋值

print ('tuple = ', tuple)           # Prints complete tuple
print ('tuple[0] = ', tuple[0])        # Prints first element of the tuple
print ('tuple[1:3] = ', tuple[1:3])      # Prints elements starting from 2nd till 3rd 
print ('tuple[-3:-1] = ', tuple[-3:-1])       # 输出结果是什么?
print ('tuple[2:] = ', tuple[2:])       # Prints elements starting from 3rd element
print ('tinytuple * 2 = ',tinytuple * 2)   # Prints tuple two times
print ('tuple + tinytuple = ', tuple + tinytuple) # Prints concatenated tuple

 结果:

tuple =  ('maxsu', 786, 2.23, 'yiibai', 70.2)
tuple[0] =  maxsu
tuple[1:3] =  (786, 2.23)
tuple[-3:-1] =  (2.23, 'yiibai')
tuple[2:] =  (2.23, 'yiibai', 70.2)
tinytuple * 2 =  (999.0, 'maxsu', 999.0, 'maxsu')
tuple + tinytuple =  ('maxsu', 786, 2.23, 'yiibai', 70.2, 999.0, 'maxsu')

 (5).Python字典

  Python的字典是一种哈希表类型。它们像Perl中发现的关联数组或散列一样工作,由键值对组成。字典键几乎可以是任何Python数据类型,但通常为了方便使用数字或字符串。另一方面,值可以是任意任意的Python对象。

  字典由大括号({})括起来,可以使用方括号([])分配和访问值。例如:

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is my"

tinydict = {'name': 'maxsu', 'code' : 1024, 'dept':'IT Dev'}


print ("dict['one'] = ", dict['one'])       # Prints value for 'one' key
print ('dict[2] = ', dict[2])           # Prints value for 2 key
print ('tinydict = ', tinydict)          # Prints complete dictionary
print ('tinydict.keys() = ', tinydict.keys())   # Prints all the keys
print ('tinydict.values() = ', tinydict.values()) # Prints all the values

 结果:

dict['one'] =  This is one
dict[2] =  This is my
tinydict =  {'name': 'maxsu', 'code': 1024, 'dept': 'IT Dev'}
tinydict.keys() =  dict_keys(['name', 'code', 'dept'])
tinydict.values() =  dict_values(['maxsu', 1024, 'IT Dev'])

 字典中的元素没有顺序的概念。但是说这些元素是“乱序”是不正确的; 它们是无序的。

2,多重赋值

a = b = c = 1
#或者
a, b, c = 10, 20, "maxsu"

3,数据类型转换

 4,随机函数

 5,字符串格式化运算符

 Python最酷的功能之一是字符串格式运算符。 这个操作符对于字符串是独一无二的,弥补了C语言中 printf()系列函数。 例如:

print ("My name is %s and weight is %d kg!" % ('Maxsu', 71))

输出结果:

My name is Maxsu and weight is 71 kg!

以下是可以与%符号一起使用的完整符号集列表:

 6,TimeTuple

Python时间函数将时间处理为9个数字的元组,如下所示:

(1).获取当前时间

要将从时间浮点值开始的秒数瞬间转换为时间序列,将浮点值传递给返回具有所有有效九个项目的时间元组的函数(例如本地时间)。

import time

localtime = time.localtime(time.time())
print ("Local current time :", localtime)

# 当前时间
curtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print (curtime)

执行结果:

Local current time : time.struct_time(tm_year=2017, tm_mon=6, tm_mday=20, tm_hour=23,
tm_min=9, tm_sec=36, tm_wday=1, tm_yday=171, tm_isdst=0)
Curtime is =>  2017-06-20 23:09:36

(2).获取一个月的日历

calendar模块提供了广泛的方法来显示年历和月度日历。 在这里,将打印一个给定月份的日历(2021年11月)

import calendar

cal = calendar.month(2021, 11)
print ("Here is the calendar:")
print (cal)

执行结果:

 November 2021
Mo Tu We Th Fr Sa Su 
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

 (3).元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。

 下面列出这种方式元组中的几个元素:

 

posted @ 2018-01-01 15:30  整合侠  阅读(640)  评论(0编辑  收藏  举报