目录
最近在跟着codecademy的 Data Science路径进行学习,实时更新一些笔记。所用编辑器为Spyder和codecademy自己的网页编辑器。
首先是一些Python基础,由于之前有过一点Python的经历,就当是复习了,内容会比较简要。
Python 句法和变量类型
在笔记本里通常可以用#来注释代码
#这是一端注释
print("Hello World!") #输出hello world
print(2) #可以输出数字
print(2+3) #输出计算结果
算数操作 (一些简单的加减乘除)
result = 10 + 30
result = 40 - 10
result = 50 * 5
result = 16 / 4
result = 25 % 2
result = 5 ** 3
result += 2
#取余 modulo 操作
mod= 12 % 5 #结果为2
变量定义 (数据类型):
user1 = "csdn" #字符串,后面还会详细说
user2 = "user"
user_id = 10 #整数 int type
balance = 100.2 #浮点数 float
verified = True #布尔
likes = None #空值
#变量值可以被覆盖
user_id = 20
字符串拼接
#字符串拼接 concatenation
user = user1 + user2 #得到 user = "csdnuser", 因为中间没有空格
Python 中的函数
Python中的函数由 def function(): 定义,其中括号内的内容为输入的参数,不同参数之间用逗号隔开,括号后面一定要记得冒号:
这里注意Python的缩进
def write_a_book(character="优快云", setting, special_skill):
#这里的character="优快云"相当于给了变量一个默认初始值)
print(character + " is in " +
setting + " practicing her " +
special_skill)
person = "Alex"
setting = "the park"
skill = "writing skill"
#call a function
write_a_book(person,setting,skill)
#也可以直接
write_a_book("Alex","the park","writing skill")
函数中要注意函数中的变量和全局变量
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
这段代码运行的结果将会是:
Python is fantastic
Python is awesome
如果需要定义一个全局变量:
x="awesome"
def myfunc():
global x
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
输出:
Python is fantastic
Python is fantastic
函数也可以返回值:
def some_function(some_input1, some_input2):
# … do something with the inputs …
return output
更具体的例子为:
def square_point(x, y, z):
x_squared = x * x
y_squared = y * y
z_squared = z * z
# Return all three values:
return x_squared, y_squared, z_squared
three_squared, four_squared, five_squared = square_point(3, 4, 5)
print(three_squared, four_squared, five_squared)
返回:
9 16 25
流程控制
elif:
pet_type = "fish"
if pet_type == "dog":
print("You have a dog.")
elif pet_type == "cat":
print("You have a cat.")
elif pet_type == "fish":
# this is performed
print("You have a fish")
else:
print("Not sure!")
if 和 else:
test_value = 50
if test_value < 1:
print("Value is < 1")
else:
print("Value is >= 1")
test_string = "VALID"
if test_string == "NOT_VALID":
print("String equals NOT_VALID")
else:
print("String equals something else!")
or , and 和 not:
True or True # Evaluates to True
True or False # Evaluates to True
False or False # Evaluates to False
1 < 2 or 3 < 1 # Evaluates to True
3 < 1 or 1 > 6 # Evaluates to False
1 == 1 or 1 < 2 # Evaluates to True
True and True # Evaluates to True
True and False # Evaluates to False
False and False # Evaluates to False
1 == 1 and 1 < 2 # Evaluates to True
1 < 2 and 3 < 1 # Evaluates to False
"Yes" and 100 # Evaluates to True
not True # Evaluates to False
not False # Evaluates to True
1 > 2 # Evaluates to False
not 1 > 2 # Evaluates to True
1 == 1 # Evaluates to True
not 1 == 1 # Evaluates to False
==:
if 'Yes' == 'Yes':
# evaluates to True
print('They are equal')
if (2 > 1) == (5 < 10):
# evaluates to True
print('Both expressions give the same result')
c = '2'
d = 2
if c == d:
print('They are equal')
else:
print('They are not equal')
!=:
if "Yes" != "No":
# evaluates to True
print("They are NOT equal")
val1 = 10
val2 = 20
if val1 != val2:
print("They are NOT equal")
if (10 > 1) != (10 > 1000):
# True != False
print("They are NOT equal")
一些比较运算符:
a = 2
b = 3
a < b # evaluates to True
a > b # evaluates to False
a >= b # evaluates to False
a <= b # evaluates to True
a <= a # evaluates to True
列表
循环
字符串
字典
类
模块