python的第一个代码,工具:PyCharm Community Edition 2025.1.1
1.python项目
1.1新建python项目
1.2.项目上右击,新建python文件
2.编写第一个python方法,打印方法
2.1简单打印
print("Dad!!")
2.2进阶打印
\ :转义符
'/":单双引号绝大数情况效果是一样的
外双内单或者外单内双都会把内的符号当作字符串的一部分输出,如果字符串中既要单引号又要有双引号,这时候就用到了转义符,在符号前加上转义符即可。
python是一行一行执行的,所以同一行代码不能换行。
“”“/‘’‘:三个双引号或三个单引号系统会自动按照文字换行进行换行,例如:
3.变量
3.1变量命名
变量名只能包含字母/数字/下划线,可以以字母或下划线打头,单不能以数字打头。
下划线命名法:字母全部小写,不同单词之间用下划线连接
驼峰命名法
变量名大小写敏感
命名不可和关键字重复
3.2字符串
title():字符串.title() 会将字符串每个单词的首字母
upper():转换大写
lower():转换小写
strip(),rstrip(),lstrip():去除两端,右,左的空格
removeprefix("abc"):去除前缀
abcremovesuffix(".txt"):去除后缀.txt
f:字符串前加f,字符串中被{}包裹的变量会被求值(引用变量值)
格式化字符串
#格式化字符串
#f方法
name="靓女"
year="2025"
message_content=f"{name},新年好"
print(message_content)
#format方法
message_content1="{strname},{stryear},新年好".format(strname=name,stryear=year)
print(message_content1)
message_content2="{0},{1},新年好".format(name,year)
print(message_content2)
3.3数
整数,浮点数
乘方:** 2**3-2的3次方
计算要先导入math库,print这种常用的系统已经帮忙导入了,称为内置函数
python官方文档: 3.13.4 Documentation
import math
math.函数名()
3.4注释
# :单行注释
"""""":三引号做多行注释
快捷键:ctrl + /
3.5数据类型
字符串 str
整型 int
浮点类型 float
布尔类型 bool
空值类型 NoneType
3.6列表及列表操作
shoping_list=["键盘","鼠标"]
# 添加内容
shoping_list.append("显示器")
print(shoping_list)
#删除内容
shoping_list.remove("鼠标")
print(shoping_list)
#一个列表可包含多种数据类型
shoping_list.append(True)
shoping_list.append(1)
shoping_list.append(2.2)
shoping_list.append(None)
print(shoping_list)
#列表长度
print(len(shoping_list))
#获取列表某个节点值
print(shoping_list[3])
#重置某个节点的值
shoping_list[4]=3.3
print(shoping_list)
#在指定位置添加元素
shoping_list.insert(2,"笔记本")
print(shoping_list)
#数字列表
num_list=[1,21,4,3,5.5,8]
#列表最大值
print(max(num_list))
#列表最小值
print(min(num_list))
#排序好的列表
print(sorted(num_list))
3.6.1 列表循环
for 变量名 in 可迭代对象 :
#对每个变量做一些事情
#缩进的代码都属于佛如循环
range(起始值,结束值,步长):整数数列,结束值不在数列内
步长代表每次跨几个数字
for + range()
3.7 条件语句
if [条件] :
n=False
if n:
print("条件为真")
else:
print("条件为假")
#嵌套判断
happy=False
in_Home=False
if happy:
if in_Home:
print("不可以打游戏")
else:
print("可以打游戏")
else:
print("不可以")
#多个条件判断elif
if 条件1:
语句A
elif 条件2:
语句B
else:
语句C
逻辑运算符:
and 并
or 或
not 不
优先级:not > and > or
3.8 字典
字典:用{}包裹,存储格式是 键:值
键 in 字典 :会返回一个布尔值,true 则说明字典中有该键
元组:l,可放多个元素,用圆括号包裹,定义之后不可变
#元组
cat_tuple1=("小猫","1号")
cat_tuple2=("小猫","2号")
#元组可以作为keu放到字典里
cat={cat_tuple1:"六六",cat_tuple2:"可乐"}
print(cat["小猫","1号"])
#字典示例
friend ={"first_name":"小颖","last_name":"陈","age": 25,
"city":"深圳"}
print("姓名:"+ friend["last_name"]+ friend["first_name"])
print("年龄:"+ str(friend["age"]))
print("城市:"+ friend["city"])
字典循环
temp_dict={"猫":"六六","狗":"可乐"}
#输出全部键
temp_dict.keys()
#输出全部值
temp_dict.values()
#输出全部键值
temp_dict.items()
#循环便利字典,staff_id对应字典内容的键,nama对应值
for staff_id,name in temp_dict.items():
if staff_id=="狗":
print(name)
for temp_tuple in temp_dict.items():
staff_id=temp_tuple[0]
name=temp_tuple[1]
if staff_id == "狗":
print(name)
3.9 函数
input()
#input函数返回字符串类型
user_age=input("请输入年龄")
#数据类型转换 字符串--》整型
age=int("user_age")
print(age)
#取余 年龄除10取余 等于0说明是10的倍数
num = age % 10
print(num)
while()
有明确循环对象或次数 --》for循环
循环次数未知 --》while循环
# while 条件A:
# 行动B
user_input = input("请输入您想要购买的商品:")
while user_input != "结账":
print("好的,购物车里会为您添加"+user_input)
user_input = input("请输入您想要购买的商品:")
3.10 定义函数
# def 函数名(参数):
# 函数代码
def calculate_sector(central_angle,radius):
#计算扇形面积
sector_area=central_angle / 360 * 3.14 * radius ** 2
print(f"此扇形面积为:{sector_area}")
calculate_sector(90,10)
calculate_sector(90,100)
作用域
变量作用域,类似Java的局部变量
函数内容返回值:使用return