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

原文地址:https://www.cnblogs.com/lizm166/p/8167680.html

时间: 2024-08-28 04:38:26

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

Python3.x基础学习-函数用法

函数用法 函数的参数类型 不可变类型参数:数值型.字符串str.元组tuple.可变类型:列表list,字典dict函数中a不可变,fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身.1.传递不可变类型参数,不会影响参数本身2.传递可变类型参数,会影响参数本身 # 不可变参数类型a =1 print(id(a)) # 2009628784 def func(a): a= 10 print(a,id(a)) func(a) # 20 2009629392 def func

Python3.x基础学习-函数用法(二)

前言 Python为我们提供了丰富的内置函数,现在我们来了解一下常用的函数 内置函数 求商和余数 divmod() ret = divmod(11,2) print(ret) # (5, 1) 求绝对值  abs() ret = abs(-100) print(ret) # 100 求最大值 max() ret = max(10,20) print(ret) # 20 # 其中 iterable 为可迭代对象,max 会 for i in … 遍历一遍这个迭代器,# 然后将每一个返回值当做参数传

Python3.x基础学习-类--类属性和实例属性

类属性和实例属性 定义: 实例属性:在实例对象中定义的属性类属性: 在类对象中定义的属性,并且多个实例对象共享一份类属性类属性使用方式:类名.类属性,也可以使用 对象.类属性注意: 1.不可变类型:对象名.属性名=属性值 是给对象添加属性,而不是进行修改 2. 可变类型:如果对象是修改可变数据类型变量中的数据,是真正的修改如果是重新给可变数据类型变量赋值,这是给该对象添加属性 class Dog: pass dog=Dog dog.name = '旺财' print(dog.name) dog.

Python3.x基础学习-类--多继承、多态

多继承 一个子类继承多个父类,就是多继承,并且拥有所有父类的属性和方法例如:孩子会继承父亲和母亲的特征 如果子类和父类中有相同的方法,就会调用子类中的方法如果不同的父类中存在有相同的方法名称,子类对象调用的时候会调用哪个父类中的方法呢?python 会根据MRO(method resolution order) 方法解析顺序列表进行查找. class Dog: def eat(self): print("吃骨头") class God: def eat(self): print(&qu

python3基础学习笔记(基础知识)-01

python的基础学习: 数据类型:python是弱类型语言,在定义变量的时候,可以不直接制定数据类型. 1.Numbers: int 有符号整型    a = (-1)  a = 1 long 长整型 float 浮点型 complex 复数 2.boolean: True    Flase 3.String :字符串   a = "Hello World" 4.List :列表 有序  list1 = [] 5.Tuple : 元组  定义后不能被修改  a = (1,2,3) 6

Python3 Tkinter基础 Entry state='readonly' 不可写入的输入框,但是可以选中与拷贝

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: from tkinter import * root = Tk() content=StringVar() content.set('可以选中') e1=Entry(root, textvariable=content, state='readonly', )

Python基础学习(九)

Python 多线程 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条来显示处理的进度 程序的运行速度可能加快 在一些等待的任务实现上如用户输入.文件读写和网络收发数据等,线程就比较有用了.在这种情况下我们可以释放一些珍贵的资源如内存占用等等. 线程在执行过程中与进程还是有区别的.每个独立的线程有一个程序运行的入口.顺序执行序列和程序的出口.

蓝鸥零基础学习HTML5—html+css基础

蓝鸥零基础学习HTML5-html+css基础 一.课程目标 1.了解前端开发职位:2.掌握常用标签以及语义及用法:3.掌握常用css的特性,掌握基础布局技巧:4.掌握整站规划概念. 二.适用人群 零基础积极学习html5者 三.课程简介 本课程主要讲解了 html+css的基础知识,包括html模板.标签.css基础样式.布局.表格表单.整站等等,是进行前端开发的基础.Html+css是前端开发的基础,大部分前端开发工程都需要从html+css布局开始,html+css的基础非常重要,是前端开

HTML&CSS基础学习笔记8-预格式文本

<pre>标签的主要作用是预格式化文本.被包围在 pre 标签中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre>标签的一个常见应用就是用来表示计算机的源代码.当然你也可以在你需要在网页中预显示格式时使用它. 会使你的文本换行的标签(例如<h>.<p>)绝不能包含在 <pre> 所定义的块里.尽管有些浏览器会把段落结束标签解释为简单地换行,但是这种行为在所有浏览器上并不都是一样的. 更多学习内容,就在码芽网http://www.