Python 数据工程简介
Python 是高级的(使其易于学习,并且不需要您了解计算机的详细信息即可使用它),通用的(可用于各种领域,例如 Web 开发、自动化、ML、 AI),解释(写入源代码)编程语言。
python 用于数据科学,因为它具有分析数据所需的丰富数学工具。
python 程序具有扩展名 .py 并由 python 文件_name.py 在命令行上运行。
Python Hello World
在这里,让我们创建我们的第一个 python 程序,hello world 程序。这将要求您首先创建一个文件夹并将其命名为mycode,您将在其中保存代码文件。然后你需要启动 VS 代码并打开你创建的文件夹,mycode。
然后创建一个新的 python 文件,将其命名为 app.py 文件并输入以下代码并保存文件。
print ('Hello World!')
进入全屏模式 退出全屏模式
print() 语句是一个内置函数,它在屏幕上返回消息,这里它返回 Hello World!屏幕上的消息。
评论。
它们以 # 开头。
在编写代码时,有时您想记录它,您想说明一段代码为何有效,您可以使用注释来做到这一点。
基本上,您使用注释来解释公式、算法和复杂的逻辑。在执行 python 程序时,python 解释器会忽略注释,只选择性地解释代码。
Python 提供了三种注释,包括块注释、内联注释和文档字符串。
- Python 块注释 这些注释解释了它下面的代码,并且与它解释的代码类似。
# Increase price of cat by 1000
price = price + 1000
进入全屏模式 退出全屏模式
- Python 内联注释 这些是与语句放在同一行的注释。
cat = cat + 1000 # increase the cat price by 1000
进入全屏模式 退出全屏模式
- **文档字符串。**文档字符串是您作为代码块的第一行放置的字符串文字,例如,函数和文档字符串称为文档字符串。从技术上讲,文档字符串不是注释,而是创建引用字符串的匿名变量。此外,Python 解释器不会忽略它们。
def sort():
""" sort the list using sort algorithm """
进入全屏模式 退出全屏模式
变量
变量是您可以为其赋值的标签。它们的唯一目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。
favorite_animal = "cat"
print(favorite_animal)
进入全屏模式 退出全屏模式
变量 favorite_animal 可以在不同的时间保存不同的值。它的价值可以在整个程序中发生变化。
算术运算
print(15 + 5) # 20 (addition)
print(11 - 9) # 2 (subtraction)
print(4 * 4) # 16 (multiplication)
print(4 / 2) # 2.0 (division)
print(2 ** 8) # 256 (exponent)
print(7 % 2) # 1 (remainder of the division)
print(11 // 2) # 5 (floor division)
进入全屏模式 退出全屏模式
比较和逻辑运算符
Python比较运算符用于比较两个值;
\u003du003d、!u003d、>、<、>u003d、<u003d。
Python 逻辑运算符
-逻辑运算符用于组合条件语句:
和,或者,不是
Python算术运算符
- 算术运算符与数值一起使用来执行常见的数学运算:+、-、、/、%、*、//
数据类型。
1.字符串
python中的字符串被单引号(')或双引号(“)包围
您可以使用 print() 函数显示字符串文字。
2.数字。
Python中有三种数字类型:
整数
漂浮
复杂的
x = 1 # int
y = 2.8 # float
z = 1j # complex
进入全屏模式 退出全屏模式
3.布尔值。
-
布尔值表示两个值之一:True 或 False
-
在 if 语句中运行条件时,Python 返回 True 或 False
#booleans
a = 1000
b = 200
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
进入全屏模式 退出全屏模式
4.列表
列表用于将多个项目存储在单个变量中。
列表是 Python 中用于存储数据集合的 4 种内置数据类型之一,另外 3 种是元组、集合和字典,它们都具有不同的质量和用途,并使用方括号[] 创建
# can store any data type
Multiple_types = [False, 5.7, "Hello"]
# accessed and modified
favourite_animals = ["cats", "dogs", "rabbits"]
print(favourite_animals[1]) # dogs
favourite_animal[0] = "parrots"
print(favourite_animal[0]) # parrots
# subsets
print(favourite_animals[1:3]) # ['cats', 'rabbits']
print(favourite_animals[2:]) # ['rabbits']
print(favourite_animals[0:2]) # ['parrots', 'dogs']
# append
favourite_animals.append("bunnies")
# insert at index
favourite_animals.insert(1, "horses")
# remove
favourite_animals.remove("bunnies")
进入全屏模式 退出全屏模式
5.字典
字典是键值对。它们被 {} 包围。字典是一个有序的集合*,可更改且不允许重复。
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
#access,modify,delete
print(thisdict["brand"]) # Ford
print(thisdict["model"]) # Mustang
del thisdict["year"]
进入全屏模式 退出全屏模式
6.循环
# With the **while** **loop** we can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
# A **for** **loop** is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# Looping Through a String
for x in "banana":
print(x)
进入全屏模式 退出全屏模式
文件 I/O
产生输出的最简单方法是使用 print 语句,您可以在其中传递零个或多个用逗号分隔的表达式。
print "Python is really a great language,", "isn't it?"
# This produces the following result on your standard screen −
Python is really a great language, isn't it?
# Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard
str = raw_input("Enter your input: ")
print "Received input is : ", str
# I typed "Hello Python!"
Hello Python
进入全屏模式 退出全屏模式
更多推荐

所有评论(0)