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()。

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

Python3.x:基础学习的更多相关文章

  1. Day1 Python基础学习

    一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编写程序,本质仍然是直接操作 ...

  2. 0007-20180403-python-自动化基础学习000--while-if 循环实操

    python-自动化基础学习000 Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64 ...

  3. Day1 Python基础学习——概述、基本数据类型、流程控制

    一.Python基础学习 一.编程语言分类 1.简介 机器语言:站在计算机的角度,说计算机能听懂的语言,那就是直接用二进制编程,直接操作硬件 汇编语言:站在计算机的角度,简写的英文标识符取代二进制去编 ...

  4. 零基础学习 Python 之数字与运算

    写在之前 大家好,这里是零基础学习 Python 系列,在这里我将从最基本的 Python 写起,然后再慢慢涉及到高阶以及具体应用方面.我是完全自学的 Python,所以很是明白自学对于一个人的考验, ...

  5. 零基础学习 Python 之前期准备

    写在之前 从今天开始,我将开始新的篇章 -- 零基础学习 Python,在这里我将从最基本的 Python 写起,然后再慢慢涉及到高阶以及具体应用方面.我是完全自学的 Python,所以很是明白自学对 ...

  6. Python基础学习二

    Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...

  7. Python基础学习之环境搭建

    Python如今成为零基础编程爱好者的首选学习语言,这和Python语言自身的强大功能和简单易学是分不开的.今天我们将带领Python零基础的初学者完成入门的第一步——环境搭建.本文会先来区分几个在P ...

  8. python基础学习9

    python基础学习 内容概要 字符编码的简介 字符编码的发展史 字符编码的实际应用 文件操作简介 文件读写模式 文件操作模式 文件操作方法 内容详情 字符编码的简介 # 字符编码主要研究的对象是文本 ...

  9. python基础学习6

    Python的基础学习6 内容概要 while + else 死循环.while的嵌套 for循环基本使用 range关键字 for循环补充.爬虫 基本数据类型及内置方法 内容详情 while + e ...

随机推荐

  1. webstorm编译less和scss

    Webstorm 配置less编译的Arguments参数: $FileName$ $FileParentDir$\ccy\ccy1\ccy2\$FileNameWithoutExtension$.c ...

  2. JavaScript基础细讲

    JavaScript基础细讲   JavaScript语言的前身叫作Livescript.自从Sun公司推出著名的Java语言之后,Netscape公司引进了Sun公司有关Java的程序概念,将自己原 ...

  3. 【linux系列】安装虚拟机时候的3中网络模式

    一.桥接 桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,那么物理网卡和虚拟网卡就相当于处于同一个网段,虚拟交换机就相当于一台实现网络中 ...

  4. [XML] CoolFormat

    http://files.cnblogs.com/files/wjs16/CoolFormat3.4.rar

  5. 交换机工作原理、MAC地址表、路由器工作原理详解

    一:MAC地址表详解 说到MAC地址表,就不得不说一下交换机的工作原理了,因为交换机是根据MAC地址表转发数据帧的.在交换机中有一张记录着局域网主机MAC地址与交换机接口的对应关系的表,交换机就是根据 ...

  6. matplotlib 散点图scatter

    最近开始学习python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下: 1.scatter函数原型 2.其中散点的形状参数marker如下: 3.其中颜色参数c如 ...

  7. localstorage - HTML 5 Web 存储总结---【巷子】

    001.localStorage概念 在html5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储,解决了cookie存储空间不足的问题(cookie中每条cookie存储 ...

  8. Oracle Schema Objects——Sequences(伪列:nextval,currval)

    Oracle Schema Objects 序列的作用 许多的数据库之中都会为用户提供一种自动增长列的操作,例如:在微软的Access数据库之中就提供了一种自动编号的增长列(ID列).在oracle数 ...

  9. SmartSprites 智能批量合并 CSS 雪碧图

    做前端的稍微有点经验的都知道 可以通过合并小图片 来减少请求数, 最早可能都是通过 fw.ps 等工具来手动合并, 这种方式的缺点就不吐槽了,效率低,可维护性差 等等 .... 一些很厉害的人,往往会 ...

  10. BBS - 文章评论

    一.文章评论 <div class="comment_region"> <div class="row"> <div class= ...