01-Python变量「精卫」
1. 理解变量
- 变量就是在计算机的内存当中开辟空间。——例如信封一样。
- 变量会被覆盖。
2. 代码的运行逻辑
- 从上到下
- 从右到左
- 最后一步,才是赋值
3. 实操
3.1 Code1
x = 1
x = x + 10
# 井号是注释,注释就是人看得见,计算机看不见。代码注解,说明。
# print: 打印、输出
print(x)
输出:
11
3.2 Code2
name1 = "lilei"
name2 = name1
print(name2)
输出:
lilei
3.3 Code3
name1 = "hanmeimei"
name1 = "AI悦创"
print(name1)
输出:
AI悦创
4. 进阶的赋值方法
a = 1
b = 1
c = 1
print(a)
print(b)
print(c)
输出:
1
1
1
a = 1
b = 1
c = 1
# print(a)
# print(b)
# print(c)
print(a, b, c) # 并列输出,默认以空格间隔
输出:
1 1 1
a = 1
b = 1
c = 1
# print(a)
# print(b)
# print(c)
print(a, b, c, sep=" ") # 并列输出,默认以空格间隔
print(a, b, c, sep=";;;")
输出:
1 1 1
1;;;1;;;1
a = 1
b = 1
c = 1
# print(a)
# print(b)
# print(c)
# print(a, b, c, sep=" ", end="\n") # 并列输出,默认以空格间隔 \n: new line 新建一行
print(a, b, c, sep=" ", end="\n\n\n\n") # 并列输出,默认以空格间隔 \n: new line 新建一行
print(a, b, c, sep=";;;", end="--------")
print(a, b, c, sep=";;;")
a = b = c = 1
print(a, b, c)
输出:
1 1 1
a, b, c = 1, 2, 3
print(a, b, c)
输出:
1 2 3
5. 练习一下
假如你有两个小孩,哥哥叫:Austin,弟弟叫:Jaden。
Austin 想喝果汁,Jaden 想喝可乐。哥哥和弟弟都有专属的杯子,不喜欢用对方的杯子。
家中仅剩一杯果汁、一杯可乐,你不小心把哥哥的杯子装了可乐,弟弟的杯子装了果汁。
问:如何交换哥哥与弟弟杯中的果汁?
zh
Suppose you have two children, the elder brother is named Austin, and the younger brother is named Jaden. Austin wants to drink juice, and Jaden wants to drink cola. Both brothers have their own exclusive cups and don't like to use each other's cups. There is only one cup of juice and one cup of cola left at home, and you accidentally filled the elder brother's cup with cola and the younger brother's cup with juice. How can you swap the juice in the brothers' cups?
Austin_cup = "Coke" # 该赋值可以理解为倒果汁的过程
Jaden_cup = "juice" # 该赋值可以理解为倒果汁的过程
print("Austin", Austin_cup)
print("Jaden", Jaden_cup)
# 待补充代码
print("Austin", Austin_cup)
print("Jaden", Jaden_cup)
两种方法思路:
方法一: 联想现实生活,现实生活中你会如何解决?
> 现实生活中,我想你会引入一个空杯子来操作,那你会如何操作?思考后再思考代码中如何实现?
方法二: Python 多个变量赋予不同值。
a, b, c = 1, 2, 3
Austin = "Coke"
Jaden = "juice"
print("Austin", Austin)
print("Jaden", Jaden)
Empty_cup = Austin
Austin = Jaden
Jaden = Empty_cup
print("-" * 12)
print("Austin", Austin)
print("Jaden", Jaden)
Austin = "Coke"
Jaden = "juice"
print("Austin", Austin)
print("Jaden", Jaden)
Austin, Jaden = Jaden, Austin
print("-" * 12)
print("Austin", Austin)
print("Jaden", Jaden)
警告!!!
不要出现如下方法!!!
Austin = "Coke"
Jaden = "juice"
print("Austin", Austin)
print("Jaden", Jaden)
# 待补充代码
print("Austin", Jaden)
print("Jaden", Austin)
Austin = "Coke"
Jaden = "juice"
print("Austin", Austin) # 输出 print(a, b, c) Austin Coke
print("Jaden", Jaden) # 输出 Jaden juice
empty_cup = Austin
Austin = Jaden
Jaden = empty_cup
print("Austin", Austin) # 输出 Austin juice
print("Jaden", Jaden) # 输出 Jaden Coke
6. 变量的命名规则
我们取名字都是有规则的,中国:姓 + 名。国外:名 + 姓;
所以,在编程当中,我们的变量命名也是有规则和要求的。
6.1 变量是区分大小写的
n = "aiyc"
N = "zhaojinwei"
print(n)
输出:
aiyc
6.2 不能使用数字开头
那,为什么不能使用数字开头呢?——数字开头,计算机会认为是数字。认为它是数字,但是数字后面又跟着一个「或几个」字母,把计算机搞懵了,分不清。
但是
除了变量的开头不能使用数字外,其他地方你想用就用!
n11111u2222m2112121212121 = 19
print(n11111u2222m2112121212121)
输出:
19
6.2 不能空格间隔
username = "aiyc"
user_name = "aiyc"
使用下划线来连接。
提示
只有下划线!!!
6.3 系统关键词不能做变量名
as = "aiyc"
# 计算机是要认为你这个 as 是关键词呢、还是变量呢?——显然,计算机不是人,就算是人看到这样的变量也会有疑惑吧。
如何查看关键词
help("keywords")
我非要用“关键词”做变量名怎么办?
As = "aiyc"
aS = "aiyc"
AS = "aiyc"
6.4 不要使用内置函数名做变量名
print = "zhaojinwei"
print(print)
快捷键
Command + /
:注释
公众号:AI悦创【二维码】
AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
- 0
- 0
- 0
- 0
- 0
- 0