今天我们开始学习python3编程之python基础

一.基础语法

变量定义规范:

python是弱类型语言,变量定义之前是不需要声明的

1.变量只能包含字母、数字和下划线,且不能以数字开头

2.变量区分大小写

3.变量名不能包含空格、连字符和特殊字符,不能使用python的关键字例如:if

查看python的关键字用以下命令查看

python 复制代码
import keyword
print(keyword.kwlist)

python的字符串引用有:" "、' '、""" """、''' '''.在python里单引号和双引号的效果是一样的

变量值对调

python 复制代码
a = 10
b = 20
a, b = b, a
print(a, b)

二.数据类型

1.number 数字类型,不可变

int 整型 :n1=10

float 浮点型 :n2=3.14

complex 复数 :1 + 1j

2.string 字符串类型,不可变

str1 = "hello"

3.bool 布尔型

b1 = True

b2 = False

4.list 列表类型,可变

list1 = [1,2,3"hello",Ture]

list2 = [1,[2,3],4]

5.tuple 元组,不可变

tuple1 = (1,2,3"hello",False)

6.dict 字典类型,可变 key:value 键值对 key唯一 key不可变

dict1 = {"name": "张三", "age": 20, "isStudent": Ture}

7.set 集合,可变 无序 不重复

set1 = {1,2,3,4,5,5,6,4,5}

print(set1)

三.数学运算

运算符号:+ - * / // % **

python 复制代码
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
13                  加法 
7                   减法
30                  乘法
3.3333333333333335  除法 # python对浮点数的精度不够
3                   整除
1                   取余
1000                幂运算

字符串运算,拼接

python 复制代码
str1 = "hello"
str2 = "world"
str3 = str1 + str2
print(str3)
helloworld

重复

python 复制代码
str4 = str1 * 3
print(str4)
hellohellohello
相关推荐
青青草原羊村懒大王5 小时前
1、pycharm相关知识
python
嫂子的姐夫5 小时前
10-七麦js扣代码
前端·javascript·爬虫·python·node.js·网络爬虫
少吃一口都不行5 小时前
脚手架学习
前端·javascript·学习
Chandler245 小时前
一图掌握 操作系统 核心要点
linux·windows·后端·系统
东风西巷5 小时前
Avast Cleanup安卓版(手机清理优化) 修改版
android·学习·智能手机·软件需求
77qqqiqi5 小时前
python循环语句
python
skywalk81635 小时前
term.everything‌ 通过终端运行任意GUI应用程序
运维
我是华为OD~HR~栗栗呀6 小时前
24届-Python面经(华为OD)
java·前端·c++·python·华为od·华为·面试
2401_841495646 小时前
【数值分析】插值法实验
python·数学·算法·可视化·数值分析·数学原理·插值法