本文简单地介绍了python的一些基本入门知识,通过对这些知识的了解,大家可以写一些简单的代码,同时也为后面深入理解打下基础。本文的主要内容如下:
值和类型
**值**,即value,通常有:1,2,3.1415,'bright','rose' **类型**,不同的值有不同的类型。值 | 类型 |
---|---|
1 | int型=整型 |
'bright' | str字符串型 |
3.1415 | float浮点型 |
type()
是判断值的类型的函数
---------Code Start------------
print(type(1))
print(type('bright'))
print(type([1, 2, 3, 4]))
print(type((1, 2, 3, 4)))
print(type({1, 2, 3, 4}))
print(type({1:'1', 2:'2', 3:'3'}))
---------Code End------------
# 结果
<class 'int'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
变量
在Python中**变量**是指向**对象**的,**变量**的类型和**赋值对象**的类型一致。name = 'bright' #name为变量指向字符串'bright'
age = 22 #age为变量指向整数22
gender = 'male'
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
操作符和操作对象
比较 | 含义 |
---|---|
操作符 | + - * / 等符号 |
操作对象 | 操作符作用的对象 |
表达式和语句
$$表达式=值、变量和操作符的组合$$ eg: 22; x; x+22 $$语句=Python解释器能运行的一个代码单元$$ eg:print(type('name')); gender = 'male'操作顺序
简单一点理解,就是直接使用`()`对其施加操作顺序。同级,从左向右执行。通常如下: - Parentheses括号 - Exponentiation乘方 - Multiplication乘法 - Division除法 - Addition加法 - Substraction减法字符串及其简单操作
字符串的乘法操作相当于字符串重复操作 字符串的加法操作相当于字符串组合 ```python str1 = 'bright' str2 = 'rose' str3 = 'like' print(str1 + str3 + str2) # brightlikerose print(str1*2 + str3*2 + str2*2) # brightbrightlikelikeroserose ```- 字符串是一个序列
- 字符串是一个对象
strx = 'bright'
print(strx) #打印字符串
for item in range(len(strx)):
print(str0[item])
函数
**函数(function)**,一些语句的组合并给个名字,函数的作用是对代码的封装。 函数需要接收参数,返回结果,一般有输入有输出,输出叫做返回值return value 常用的函数: - type() - int() - float() - str()列表
- 列表是一个值得序列 - 列表是一个对象 - 列表是一个可以容纳万物的容器 - 列表可以嵌套print([1,2,3,4,5])
# 结果:[1, 2, 3, 4, 5]
print(['bright','23','rose',21,3.1415926])
# 结果:['bright', '23', 'rose', 21, 3.1415926]
print([[1,2,3],['a','b','c'],[1.1,1.2,1.3]])
# 结果:[[1, 2, 3], ['a', 'b', 'c'], [1.1, 1.2, 1.3]]
字典
- 可以理解为带下表的列表,下标为键,可以是大部分对象 - 键:值 - dict()定义字典 - 键值映射 - 可以嵌套dicttem = dict()
print(dicttem) # {}
dicttem['1'] = 'one'
dicttem['2'] = 'two'
dicttem['3'] = 'three'
print(dicttem) # {'1': 'one', '2': 'two', '3': 'three'}
print(len(dicttem)) #打印键的长度 # 3
元组
- 不可变 - 是一个序列 - 是一个对象 - tuple() - ,逗号也可定义t = 1,2,3,4,5
print(t) # (1, 2, 3, 4, 5)
print(t[0]) # 1