「Python」Python 01 基础语法

「 生活不易,我用python 」

① Python 基本数据类型

  • Number「int、float、bool、complex」
  • String(字符串)
  • List(列表)
    1.列表中每个元素可变;
    2.列表可存储 Python 任何对象;᨝
  • Tuple (元组)
    1.固定版本列表,元组不可修改;
  • Dictionary(字典)
    1.数据必须以键值对形式出现;
    2.键(key)不能重复,且不可变,值(Value)可变,可以是任何对象
  • sets(集合:无序,不重合的任意对象)

② Python 条件控制

age = 30
if age >= 20:
    print('Young')
elif age >= 40:
    print('Man')
else:
    print('else')

③ Python 循环

○ for 循环:

lists = ['n1', 'n2', 'n3', 'n4']
for i in lists:
    print(i)

○ While 循环

i = 0
size = 5
while i < size:
    print('i =', i)
    i += 1

④ Python 函数

函数通用格式:

def 函数名(参数列表):
  函数体

实例

def hello():
    print('Hello,Python3')
hello() # 函数调⽤

⑤ Python 类

类的继承「Python 类可以多继承」

⑥ Python 模块

Python 模块理解:Java 语言 包「package」 类似,使用 Import 语句导入

⑦ Python 输入输出

输出:Print()
读取键盘输入:input()

⑧ Python 文件

基本格式:

file = open(filename, mode)

文件对象方法:

file.read()
file.readline()
file.readlines()
file.close()

读取文件:

file = open('F:/install_notes_cn.txt', 'r')
for line in file.readlines():
    print(line)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容