测绘之路

PYTHON笔记

2023-11-10
编程学习
Python
note
最后更新:2024-09-19
7分钟
1223字

一、基础语法

1.基础数学函数

1
from math import *
2
#withNumber
3
num=-6
4
print(num)
5
print(floor(6,2))#floor函数:剪切掉小数点后的数字
6
print(ceil(3.3))#ceil函数:直接直接进位
7
print(sqrt(36))#sqrt函数:求平方根

2.用户输入

1
name = input("Enter your name: ")
2
age = input("Enter your age ")
3
print("Hello" + name + "! You are " + age)
4
5
#SetupCal
6
num1=input("Enter your first numer")
7
num2=input("Enter your second numer")
8
result=float(num1) + float(num2)#float:将其转换成浮点数
9
print(result)
10
11
#填空游戏
12
color = input("Enter a color: ")
13
plural_noun = input("Enter a Plural_noun")
14
celebrity = input("Enter a celebrity: ")
15
3 collapsed lines
16
print("Roses are " + color)
17
print(plural_noun + " are blue ")
18
print(" I love " + celebrity )

3.列表

1
friends = ["Tom", "Sam", "Joey", "Jim", "Wu"]
2
3
print(friends[1])#读取小标为1的数据
4
print(friends[1:])#从下标为1的数据开始读取
5
print(friends[1:4])#从下标为1的数据读取到下标为4的数据但不包括4
6
7
#列表的功能
8
friends = ["Tom", "Sam", "Joey", "Jim", "Wu"]
9
luck_numbers = [4, 6, 2, 4, 8]
10
#print(friends[1])
11
#print(friends[1:])
12
#print(friends[1:4])
13
#friends.extend(luck_numbers)
14
friends.append("Creed")#添加单个数据
15
friends.insert(2,"Aili")#在第三个位置插入数据
13 collapsed lines
16
friends.remove("Jim")#删除指定内容
17
#friends.clear()
18
friends.pop()#删掉最后一个数据
19
print(friends)
20
print(friends.index("Wu"))#输出指定元素下标
21
print(friends.count("Tom"))#输出指定元素的数量
22
friends.sort()#对元素进行升序排序
23
print(friends)
24
luck_numbers.reverse()#将数据颠倒输出
25
print(luck_numbers)
26
27
friends2 = friends.copy()#复制数据
28
print(friends2)

4.元组

1
coordinates = (4,6,2,7)#元组
2
coordinates[1] = 10#错误无法更改数据
3
print(coordinates[1])

4.1二者之间的区别

Python中的列表和元组都是用来存储一组数据的数据类型,但它们有以下区别:

  1. 列表是可变的,元组是不可变的。也就是说,列表中的元素可以被修改、添加或删除,而元组中的元素不能被修改。
  2. 列表使用方括号 [] 来表示,元组使用圆括号 () 来表示。
  3. 列表通常用于存储同类型的数据,而元组通常用于存储异构的数据。
  4. 列表的操作速度比元组慢,因为列表需要动态分配内存,而元组在创建后就不可修改,因此可以更快地访问。
  5. 列表可以作为函数的参数传递,而元组通常用于函数的返回值。

总之,列表和元组都有各自的优缺点,应根据具体情况选择使用哪种数据类型。

5.语法结构

5.1条件语句

1
is_male = True
2
is_tall = False
3
if is_male and is_tall:#两者都为真
4
print("You are a male or tall or both")
5
elif is_male and not(is_tall):#前为真,后为假
6
print("You are a short male")
7
elif not (is_male) and is_tall:#前为假后为真
8
print("You are not a male but are tall")
9
else:#both is false
10
print("You neither male nor tall")

5.2比较语句

1
def max_num(num1,num2,num3):
2
if num1 >= num2 and num1 >=num3:
3
return num1
4
elif num2 >= num1 and num2 >= num3:
5
return num2
6
else:return num3
7
8
print(max_num(3,49,5))

5.3建立计算器

1
#从用户处输入数据
2
num1 = float(input("Enter first number: "))
3
op = input("Enter operator: ")#根据输入符号判断进行的运算
4
num2 = float(input("Enter second number: "))
5
6
if op == "+ ":
7
print(num1 + num2)
8
elif op =="-":
9
print(num1 - num2)
10
elif op == "/":
11
print(num1/num2)
12
elif op == "*":
13
print(num1 * num2)
14
else:
15
print("Invalid operator")

5.4while循环

1
i = 1
2
while i <= 10:
3
print(i)
4
i +=1
5
6
print("Done with loop")

5.5猜字游戏

1
secret_word = "giraffe"
2
guess = ""
3
guess_count = 0
4
guess_limit = 3
5
out_of_guesses = False
6
while guess != secret_word and not(out_of_guesses):
7
if guess_count < guess_limit:
8
guess = input("Enter your guess: ")
9
guess_count +=1
10
else:
11
out_of_guesses = True
12
if out_of_guesses:
13
print("Out of Guesses, YOU LOSE")
14
else:
15
print("You win!")

6.建立字典

1
monthConversions =
2
{
3
"Jan": "January",
4
"Feb": "February",
5
"Mar": "March",
6
}
7
8
print(monthConversions["Mar"])
9
#print(monthConversions.get("Mar"))

通过Mar关键字打印出March

7.列表与嵌套循环

1
number_grid = [
2
[1,2,3],
3
[4,5,6],
4
[7,8,9],
5
[0]
6
]
7
8
#print(number_grid[0][2])
9
10
#for row in number_grid:
11
# print(row)
12
13
for row in number_grid:
14
for col in row:
15
print(col)

8.捕捉错误

1
try:
2
#Value = 10/0
3
number = int(input("Enter a number: "))
4
print(number)
5
except ZeroDivisionError:
6
print("Divided by zero")#错误输入
7
except ValueError:
8
print("invalid input")

二、函数

1.函数基本声明调用

1
def say_Hi(name):
2
{
3
print("Hello User" + name)
4
}
5
6
print("Top")
7
say_Hi(" Tom")
8
print("Buttom")

2.return语句

1
def cube(num):
2
return num*num*num
3
result = cube(4)
4
print(result)

3.指数函数

1
def raise_to_power(base_num, pow_num):
2
result = 1
3
for index in range(pow_num):
4
result = result * base_num
5
return result
6
7
print(raise_to_power(2,4))

4.建立翻译

1
def translate(phrase):#读入phrase字符串
2
translate = ""
3
for letter in phrase:
4
if letter.lower() in "aeiou":
5
if letter.isupper():#判断大小写
6
translate = translate + "G"
7
else:
8
translate = translate + "g"
9
else:
10
translate = translate + letter
11
return translate
12
13
14
print(translate(input("Enter a phrase: ")))

5.模块

5.1被调用模块

useful_tools

1
def get_file_ext(filename):
2
return filename[filename[filename.index(".") + 1]]
3
4
def putnum(num):
5
return num+10
6
#对输入进来的数进行加十操作

5.2调用

1
import useful_tools#调用useful_tools模块
2
3
#print(useful_tools.get_file_ext("bsjfsa"))
4
print(useful_tools.putnum(10))

三、文件操作

1.读文件

1
employee_file = open("pythonProject\\测试.txt", "r")
2
3
'''
4
print(employee_file.read())#读全部文件
5
print(employee_file.readline())#读一行
6
'''
7
8
print(employee_file.readlines())#分成数组读出
9
employee_file.close()

2.写文件

1
employee_file = open("pythonProject\\测试2.txt", "w")
2
3
employee_file.write("\nTobe,a man")
4
employee_file.close()

四、类与对象

1.创建类

1
class Student:
2
def __init__(self, name, major, gpa, is_on_probation):
3
self.name = name
4
self.major = major
5
self.gpa = gpa
6
self.is_on_probation = is_on_probation
7
#__init__是python指定的构造函数写法,self类似于c 中的this指针

调用类创建对象

1
from Student import Student
2
3
student1 = Student("Jim", "Business", 4.1, False)
4
student2 = Student("Pam", "Art", 4.5, True)
5
print(student2.gpa)
6
print(student1.is_on_probation)

2.继承

父类

1
class Chef:
2
3
def make_chicken(self):
4
print("Te chef makes a chicken")
5
6
def make_salad(self):
7
print("The chef makes a salad")
8
9
def make_special_dish(self):
10
print("The chef make a special_dish")

子类

1
from Chef import Chef
2
3
class ChineseChef(Chef):
4
5
def make_fried_rice(self):
6
print("The chef mkes fried rice")
7
8
def make_special_dish(self):
9
print("The chef makes orange chicken")

调用

1
from Chef import Chef
2
from ChineseChef import ChineseChef
3
4
myChef = Chef()
5
myChef.make_special_dish()
6
7
myChineseChef = ChineseChef()
8
myChineseChef.make_chicken()
9
myChineseChef.make_special_dish()
本文标题:PYTHON笔记
文章作者:测绘王同学
发布时间:2023-11-10