Python3基础知识

Python基础知识

1值和类型

,即value,通常有:1,2,3.1415,'bright','rose'

类型,不同的值有不同的类型:值1类型为整数,'bright'类型为字符串。

type('rose') #判断值'rose'的类型函数
str
type(3.1415) 
float
type('3.1415')
str

2变量

在Python中变量是指向对象的。

变量的类型和赋值对象的类型一致。

name = 'bright' #name为变量指向字符串'bright'
age = 22        #age为变量指向整数22
gender = 'male'
print(type(name))
print(type(age))
<class 'str'>
<class 'int'>

3操作符和操作对象

操作符,+ - * / 等符号

操作对象,操作符作用的对象

4表达式和语句

表达式,值、变量和操作符的组合

eg: 22; x; x+22

语句,Python解释器能运行的一个代码单元

eg:print(type('name')); gender = 'male

5操作顺序

  • Parentheses括号

    扫描二维码关注公众号,回复: 26703 查看本文章
  • Exponentiation乘方

  • Multiplication乘法

  • Division除法

  • Addition加法

  • Substraction减法

同级,从左向右执行

6字符串简单操作

字符串的乘法操作相当于字符串重复操作

字符串的加法操作相当于字符串组合

str1 = 'bright'
str2 = 'rose'
str3 = 'like'
print(str1 + str3 + str2)
print(str1*2 + str3*2 + str2*2)
brightlikerose
brightbrightlikelikeroserose

7函数

函数(function),一些语句的组合并给个名字

函数需要接收参数,返回结果,一般有输入有输出,输出叫做返回值return value

常用的函数:

  • type()
  • int()
  • float()
  • str()

8字符串

  • 字符串是一个序列
  • 字符串是一个对象
str0 = 'bright' 
print(str0) #打印字符串
for item in range(len(str0)):
    print(str0[item])
bright
b
r
i
g
h
t

9列表List

  • 列表是一个值得序列
  • 列表是一个对象
  • 列表是一个可以容纳万物的容器
  • 列表可以嵌套
print([1,2,3,4,5])
print(['bright','23','rose',21,3.1415926])
print([[1,2,3],['a','b','c'],[1.1,1.2,1.3]])
[1, 2, 3, 4, 5]
['bright', '23', 'rose', 21, 3.1415926]
[[1, 2, 3], ['a', 'b', 'c'], [1.1, 1.2, 1.3]]

10字典

  • 可以理解为带下表的列表,下标为键,可以是大部分对象
  • 键:值
  • dict()定义字典
  • 键值映射
  • 可以嵌套
dicttem = dict()
print(dicttem)
dicttem['1'] = 'one'
dicttem['2'] = 'two'
dicttem['3'] = 'three'
print(dicttem)
print(len(dicttem)) #打印键的长度
{}
{'1': 'one', '2': 'two', '3': 'three'}
3

11元组

  • 不可变
  • 是一个序列
  • 是一个对象
  • tuple()
  • ,逗号也可定义
t = 1,2,3,4,5
print(t)
print(t[0])
(1, 2, 3, 4, 5)
1

猜你喜欢

转载自www.cnblogs.com/brightyuxl/p/8858121.html