Python Data Science 学习笔记(一)Python基础

本文概述了Python的基础知识,包括变量类型(如字符串、数字和布尔),函数的定义与调用,以及流程控制(if-else、elif、逻辑运算)。通过Codecademy的学习笔记,介绍了列表、字符串操作、字典、类、模块和文件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

Python 句法和变量类型

Python 中的函数

流程控制

列表 

循环 

字符串 

字典 

类 

模块 

文件 


最近在跟着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

列表 

 

循环 

 

字符串 

 

字典 

 

类 

 

模块 

 

文件 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值