AI, 开发

Python学习笔记

参考资料

https://www.bilibili.com/cheese/play/ep429429

准备

安装 Python

前往 Python 官网下载 python 程序并安装到电脑。

安装 PyCharm

前往 PyCharm 官网下载 pycharm 程序并安装到电脑。安装 community 版本即可,永久免费使用。

在插件选项卡中搜索 chinese 安装中文语言包,选中 Chinese (Simplified) Language Pack。安装完成后重启软件即可。

初次尝试

点击创建项目按钮,设置项目相关信息并选择一个 phthon 版本后创建。

在文件目录中,右键点击根目录文件夹,选择 “新建” – “文件”,输入文件名称,如 hello.py。创建一个新的文件。

// hello.py
print('Hello World!!!')

在编辑区域点击右键,选择运行即可运行编写的代码。

// 运行结果
/Users/liufangwei/Documents/python/first/pythonProject/.venv/bin/python /Users/liufangwei/Documents/python/first/pythonProject/hello.py 
Hello World!!!

进程已结束,退出代码为 0

退出代码为 0 表示该代码没有出错,如果出错会为一个非 0 的数字。

基本语法

注释

# 我是注释

字符串

// 字符串连接
print('Hello' + ' World' + '!!!')

// 字符串中使用双引号
print('He said "good!"')

// 字符串中使用单引号
print("He said 'good!'")

// 字符串中使用单双引号
print("He said \"let\'s go!\"")

// 字符串换行
print("Hello!\nHi!")

// 三引号换行
print('''Hello!
Hi!''')

变量

命名规则:只能用字母,数字与下划线三种元素组成,且第一个字符不可为数字。变量名不可占用python关键字

my_love = 'xuan'
print('Hello' + my_love)

运算

1 + 2
2 - 1
2 * 3
2 / 4
2 ** 3 // 2的三次方

复杂运算

导入 math 函数库

支持的函数可查询 python 官方文档 math

import math
math.sin( 1 )

数据类型

字符串
'Hello'

整数
6

浮点数
6.0

布尔类型
True / False

空值类型
None

列表/数组
[ 'a', 'b', 'c' ]

数据操作

type函数可以返回数据类型

type(6)

len函数可获取长度

len('Hello')

索引

'Hello'[3]

数据类型转换

int("666")
float("6.66")
str(666)

input()

user_age = input("请输入您的年龄")
print( user_age )

append()

the_array = [ 'a', 'b' ]
the_array.append( 'c' )

remove()

the_array = [ 'a', 'b', 'c' ]
the_array.remove( 'c' )

条件判断

if True:
  print('Yes')

if 3 >= 6:
  print('No')
else:
  print('Yes')

// 多条件判断
if True:
  if True:
    print('Yes')
  else:
    print('No')
else:
  print('No')

// 分支判断
if a = 1:
  [结果]
elif a = 2:
  [结果]
elif a = 3:
  [结果]
else:
  [结果]

// 逻辑运算
and, or, not
if x > 5 and x < 10:
  [结果]
内容目录

PUJI Design 朴及设计 (c) 2024. 沪ICP备17052229号