第1天:
1. 了解Python的基本概念,如语法规范、解释器等
2. 学习Python数据类型(字符串、数字、列表、元组、字典)
3. 安装Python并配置开发环境
4. 编写第一个Python程序,输出"Hello World"
了解Python的基本概念,如语法规范、解释器等
Python的其他一些基本概念:
1. 变量和数据类型:Python是一门动态类型语言,不需要指定变量类型。支持的数据类型有:- 数字:int, float, complex
- 字符串:str
- 布尔型:bool
- 列表:list
- 元组:tuple
- 集合:set
- 字典:dict定义变量使用 =,例如:
python
x = 5 # 整型
y = 3.14 # 浮点型
name = 'John' # 字符串
2. 运算符:Python支持各种标准运算符:+ - * / % ** //等数学运算符,**指数运算符,//地板除法。
还有逻辑运算符:and or not, 比较运算符:> < == <= >= != 等。
3. 条件语句和循环:Python有if/else条件语句和for/while循环语句:
python
if age > 18:
print('adult')
elif age > 13:
print('teenager')
else:
print('child')
for i in range(5): # 迭代0-4
print(i)
补充range的知识:
range() 是 Python 中的一个内置函数,用于生成整数序列。语法:
python range(start, stop, step)
- start: 整数序列的起始值,默认为0
- stop: 整数序列的结束值(不包含该值)
- step: 整数序列之间的步长,默认为1例子:
python # 从0开始到5,步长为1 range(5) # [0, 1, 2, 3, 4] # 从2开始到10,步长为2 range(2, 11, 2) # [2, 4, 6, 8, 10] # 从0到-10,步长为-2 range(0, -11, -2) # [0, -2, -4, -6, -8] # 只写一个参数,起始值为0,结束值为该参数 range(5) # [0, 1, 2, 3, 4]
range() 生成的是一个range对象,需要调用list() 转化成列表使用:
python a = range(5) # range(0, 5) b = list(a) # [0, 1, 2, 3, 4]
range() 在 Python 2 和 Python 3 的实现有一点差异:- 在 Python 2 中,range() 返回一个列表
- 在 Python 3 中,range() 返回一个 range 对象所以在 Python 3 里如果想得到一个列表,需要加 list() 转换。
4. 函数:Python函数定义使用def关键字:
python
def say_hello(name):
return 'Hello ' + name
say_hello('John') # 返回'Hello John'
def比较重要再详细的学一学:
在Python中,def 关键字用于定义函数。一个基本的函数定义如下:
python def my_function(): print("Hello from a function!")
这个函数名为my_function,没有参数,并打印一条信息。我们可以给函数传递参数:
python def my_function_with_args(username, greeting):