今天我们开始学习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
相关推荐
fanged28 分钟前
Agent的Skills(TODO)
学习
是隼人40 分钟前
buuctf-pwn picoctf_2018_shellcode(ret2shellcode)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
XZ-07000142 分钟前
week4-1-figure画布
python
小马同学-1 小时前
OpenStack 使用实战:Web 界面与 CLI 命令行实验
运维·云计算·openstack
GeW1 小时前
RHCE备考别瞎学!按这个计划走,30天提高通过率,快速拿证
linux
m4Rk_1 小时前
【论文阅读】Agent 记忆机制(69):STITCH——用上下文意图解决“语义相关但情境错误”的记忆检索
论文阅读·人工智能·学习·开源·github
2601_949950631 小时前
个人在线刷题的工具
学习·考研·小程序·刷题·小程序推荐
Escalating_xu1 小时前
【System V 信号量】从 P/V 原语到 Builder 封装:写出可控、可清理的进程互斥组件
java·linux·开发语言·jvm
步行cgn1 小时前
Spring 注入 Map 集合详解
数据库·python·spring
傲世仙尊1 小时前
System V 进程间通信详解:共享内存、消息队列与信号量(CSDN博客)
linux·开发语言·c++