py速成学习

[TOC]

python

print

  1. 使用print

    • hello world

      1
      print("hello world")

      值得注意的是使用py的打印函数是printf并没有f

      py是一门脚本语言所以我们写一句话也是可以运行出结果的

    • 字符串怕拼接

      1
      print("hello"+" world")
    • 换行和打印多行

      1
      2
      3
      4
      print("""床前明月光,
      疑是地上霜。
      举头望明月,
      低头思故乡。""")

      这个是和c不同的

赋值和创建一个变量

1
2
str="this is a string"
print(str)
1
2
3
4

str1 = "string "
str2=str1
print(str1) # string

运算符知识

  1. 基本和C语言一致

  2. 不同或补充

    • **乘方符号

      1
      2**3  # 2的3次方

导入库

  1. 使用import

    1
    2
    3
    # 例如
    import math
    # 导入math的库,这个库是与数学计算有关的库
    1
    2
    from math import sin
    # 从math中导入sin

基础数据类型

  1. 字符串

    基本和c++一样

    • len

      求字符串长度

    • []

      使用索引得到字符

  2. 浮点型只有float

  3. 布尔类型

    必须是大写开始

    True

    False

  4. 使用type()打印当前变量的类型

获取用户的输入

  1. 使用input

    1
    2
    int_num=int(input("请输入一个整数:"))
    print(int_num)

    使用input读取用户的输入是一个string,所以我们使用int()函数转换成int才是我们需要的Int_num

    同样的转换函数还有很多比如float等都是将

判断语句

1
2
3
4
5
6
7
8
9
10
11
12
# 判断语句

# is_true=bool(input("input bool:"))
int_num=int(input("please input a num:"))


if int_num<0:
print("<0")
elif 0<=int_num<100:
print("<=0 and <100") # 请注意判断的写法
else:
print(">=100")

逻辑运算

  1. 与C语言不一样

    • and 与 == &
    • or 或 ==|
    • not 非 ==!

    所谓的不同都是符号发生了变化,本质并没有很大的变化

列表

  1. 列表是可变的

  2. 创建和使用列表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 列表

    list=["信安","实验班","物理网","网工"]
    print(list)
    # 添加元素
    list.append("网安")
    print(list)
    # 删除元素
    list.remove("网安")
    print(list)
    # 求长度
    print(len(list))
    # 通过索引得到元素
    print(list[1])

键值对的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
# 普通的键值对
key_value1={"chg":1,
"zjy":2,
"test":3}
print(key_value1["chg"])

print(len(key_value1))# 有几个键值对
key_value1["test2"]=4 # 添加键值对
print(key_value1["test2"])

# 判断是否存在
if "xxxx" in key_value1:
print(key_value1["chg"])
else:
print("don't hava")

# 加入元组的键值对,元组不可更改
key_value2={(1,"chg"):"man",
(2,"test1"):"man",
(3,"test2"):"woman"}

for k in key_value2.items():
print(k)

循环

  • for

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    # 键值对和元组的使用

    # 普通的键值对
    key_value1={"chg":1,
    "zjy":2,
    "test":3}
    print(key_value1["chg"])

    print(len(key_value1))# 有几个键值对
    key_value1["test2"]=4 # 添加键值对
    print(key_value1["test2"])

    # 判断是否存在
    if "xxxx" in key_value1:
    print(key_value1["chg"])
    else:
    print("don't hava")


    # 使用循环打印键值对
    for name in key_value1.keys(): # .key()方法是读取出所有的key。还有/value是读取所有值。使用.item是读取所有的键值对
    print(name)


    for key_value in key_value1.items():#读取出来是一个元组
    print(key_value)
    for num in key_value1.values():
    print(num)


    total = 0
    for i in range(0,101): # 范围是0~100,括号左边的取不到 range()还有第三个参数表示步长
    total+=i

    print(total)

  • while()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    is_true=True
    i=0
    while is_true:
    i+=1
    if i==100 :
    is_true=False


    print(i)

格式化字符串

1
2
3
4
5
6
7
name="chg"
school="CUIT"
string_test1="""{name} is a student in {school}""".format(name=name,school=school)
print(string_test1)

string_test2="this is a test {0}".format("string")
print(string_test2)

image-20230714093050803

定义函数

1
2
3
4
def add(a,b):
return a+b

print(int(add(1,3)))

导入第三方库

  1. 通过pypi.org网站查看库
  2. 通过pip install 仓库名安装
  3. 使用import引入

面向对象

  1. 使用对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
       
    class Student:
    def __init__(self,id,name):
    self.id=id
    self.name=name
    self.grades={"语文":0,
    "数学":1,
    "英语":2}
    def set_grades(self,course,grade):
    if course in self.grades:
    self.grades[course]=grade
    def print_grades(self):
    for course in self.grades:
    print(f"{course}的成绩是{self.grades[course]}")

    chg=Student("10010","chg")
    print(chg.id)
    print(chg.grades)
    chg.set_grades("数学",122)
    print(chg.grades)
    chg.print_grades()
  2. 继承

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    class Student:
    def __init__(self,id,name):
    self.id=id
    self.name=name
    self.grades={"语文":0,
    "数学":1,
    "英语":2}
    def set_grades(self,course,grade):
    if course in self.grades:
    self.grades[course]=grade
    def print_grades(self):
    for course in self.grades:
    print(f"{course}的成绩是{self.grades[course]}")
    def do_homework(self):
    print("做作业")

    class Pupils(Student):
    def __init__(self,id,name,other_subjects):
    super().__init__(id,name)# 调用构造函数
    self.other_subjects=other_subjects




    chg=Student("10010","chg")
    print(chg.id)
    print(chg.grades)
    chg.set_grades("数学",122)
    print(chg.grades)
    chg.print_grades()


    # 创建子类
    test_student=Pupils(244,"test_student","C语言")
    print(test_student.other_subjects)


文件

文件读取的模式有一下几种

image-20230714145153111

  1. 读文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    file=open("C:\\Users\\12414\\Desktop\\test.txt","r",encoding="utf-8")

    print(file.readline())# 读一行
    print(file.read(10))
    file.seek(0)


    # 使用while读取文本
    str=file.readline()
    while str!="":
    print(str)
    str=file.readline()

    # 使用列表
    file.seek(0)
    lines=file.readlines()
    for line in lines:
    print(line)

    file.close()


    with open("C:\\Users\\12414\\Desktop\\test.txt","r",encoding="utf-8") as file:# 执行完后会自动关闭
    print(file.readline())


  2. 写文件

    1
    2
    3
    4
    with open("C:\\Users\\12414\\Desktop\\test.txt","w",encoding="utf-8") as file:# 遍历
    file.write("this a string")



py速成学习
https://tsy244.github.io/2023/07/13/python/py基础学习/
Author
August Rosenberg
Posted on
July 13, 2023
Licensed under