Python 学习(一)

简介: 1. 保留字['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', ...

1. 保留字

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
查看方法:

img_aca5ae5b87a35725e0a13e2bbcf49ebf.png
图1.png

2. 打印

1). 写代码

# !/usr/bin/python3

# 第一个注释
print ("Hello, Python!")
img_10b8d964b3113fc72fef42d2ad0a8138.png
图2.png

2). 命令行运行

python helloPython.py

打印结果:


img_e010ddf8cd7324df9262151d6ccd3653.png
图3.png

3. 注释

注释可以使用#进行单行注释或者'''"""进行多行注释

# !/usr/bin/python3

# 第一个注释
print ("Hello, Python!")

# 第一个注释
'''
第二个注释
'''
"""
第三个注释
"""
img_a6f7256249559eaaeb8dfe161b5deeaa.png
图4.png

4. 缩进

python最具特色的就是使用缩进来表示代码块,不需要使用大括号 {} 。
缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。

if True:
    print ("True")
else:
    print ("False")

img_fd5b478620aaf62e6850511ded89487d.png
图5.png

注: 缩进不一致,会导致运行错误

5. 多行语句

1). Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠()来实现多行语句

total = item_one + \
        item_two + \
        item_three

2). 在 [], {}, 或 () 中的多行语句,不需要使用反斜杠()

total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']

6. 数据类型

1). 数字有四种类型:整数、布尔型、浮点数和复数

int(整数:1) 
boolean(布尔类型:true)  
float(浮点型:1.23)  
complex(复数:1.1+2.2j)

2). 字符串

- python中单引号和双引号使用完全相同。
- 使用三引号('''或""")可以指定一个多行字符串。
- 转义符 '\'
- 反斜杠可以用来转义,使用r可以让反斜杠不发生转义。。 如 r"this is a line with \n" 则\n会显示,并不是换行。
- 按字面意义级联字符串,如"this " "is " "string"会被自动转换为this is string。
- 字符串可以用 + 运算符连接在一起,用 * 运算符重复。
- Python 中的字符串有两种索引方式,从左往右以 0 开始,从右往左以 -1 开始。
- Python中的字符串不能改变。
- Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。
- 字符串的截取的语法格式如下:变量[头下标:尾下标]

示例:

str='Mazaiting'

# 输出字符串
print(str)
# 输出第一个到倒数第二个的所有字符
print(str[0:-1])
# 输出字符串第一个字符
print(str[0])
# 输出从第三个开始到第五个字符
print(str[2:5])
# 输出从第三个开始的所有字符
print(str[2:])
# 输出字符串两次
print(str * 2)
# 连接字符串
print(str + '你好')

print('-----------------------------')

# 使用反斜杠(\)+n转义特殊字符
print('hello\nworld')
# 使用字符串前面添加一个r,表示原始字符串,不会发生转义
print(r'hello\nworld')
img_1ce8073e7cab244408638a14deb7b407.png
图6.png

打印结果:


img_4fd6512e5c9b5112e455c7a8b0116a82.png
图7.png

7. 等待用户输入

input("\n\n按下 enter 键后退出.")

8. 同一行显示多条语句

import sys; x = 'mazaiting'; sys.stdout.write(x + '\n')

9. 代码组

  • 缩进相同的一组语句构成一个代码块,我们称之代码组。
  • 像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组。
  • 我们将首行及后面的代码组称为一个子句(clause)。
if expression : 
   suite
elif expression : 
   suite 
else : 
   suite

10. 输出

x = "a"
y = "b"
# 换行输出
print(x)
print(y)
# 不换行输出
print(x, end = " ")
print(y, end = " ")

打印结果:


img_ff755c3353350583e51a19c643c248da.png
图8.png

11. import 与 from...import

  • 在 python 用 import 或者 from...import 来导入相应的模块。
  • 将整个模块(somemodule)导入,格式为: import somemodule
  • 从某个模块中导入某个函数,格式为: from somemodule import somefunction
  • 从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc
  • 将某个模块中的全部函数导入,格式为: from somemodule import *
    示例1:
# 全部导入
import sys

print('=====================Python import mode====================')

print('命令行参数为:')

for i in sys.argv:
    print(i)

print ('\n Python 路径为 ', sys.path)

打印结果:


img_d2e70ac35716e4a81709c044f9832b5a.png
图9.png

示例2:

# 导入sys模块的argv,path成员
from sys import argv,path
print('=====================Python import mode====================')

print ('path: ', path)

打印结果:


img_8fc7edf6d4fd86a57ccc7265b9b5bb31.png
图10.png
目录
相关文章
|
12天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
16天前
|
Python
Python文件操作学习应用案例详解
【4月更文挑战第7天】Python文件操作包括打开、读取、写入和关闭文件。使用`open()`函数以指定模式(如'r'、'w'、'a'或'r+')打开文件,然后用`read()`读取全部内容,`readline()`逐行读取,`write()`写入字符串。最后,别忘了用`close()`关闭文件,确保资源释放。
17 1
|
8天前
|
Python
python学习3-选择结构、bool值、pass语句
python学习3-选择结构、bool值、pass语句
|
6天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
17 0
|
7天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
44 0
|
8天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
8天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
8天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
8天前
|
Python
python学习10-函数
python学习10-函数
|
8天前
|
存储 索引 Python
python学习7-元组
python学习7-元组