学习python第一天

1.输出
python 复制代码
print("Hello, World!")
2.退出命令提升符
python 复制代码
exit()
3.Python 缩进

实例

python 复制代码
if 5 > 2:
  print("Five is greater than two!")

++空格数取决于程序员,但至少需要一个。++

您必须在同一代码块中使用相同数量的空格,否则 Python 会出错:

错误实例

python 复制代码
if 5 > 2:
 print("Five is greater than two!") 
        print("Five is greater than two!")
4.Python 中的注释:
a.行注释
python 复制代码
#This is a comment.
print("Hello, World!")
b.块注释
python 复制代码
"""
This is a comment
written in 
more than just one line
"""
print("Hello, World!")
5.变量
a.与其他编程语言不同,Python 没有声明变量的命令。
b.变量不需要使用任何特定类型声明,甚至可以在设置后更改其类型。
python 复制代码
x = 5 # x is of type int
x = "Steve" # x is now of type str
print(x)

输出只有'Steve'

c.字符串变量可以使用单引号或双引号进行声明:
python 复制代码
x = "Bill"
# is the same as
x = 'Bill'
d.变量名称

变量可以使用短名称(如 x 和 y)或更具描述性的名称(age、carname、total_volume)。

Python 变量命名规则:

  • 变量名必须以字母或下划线字符开头
  • 变量名称不能以数字开头
  • 变量名只能包含字母数字字符和下划线(A-z、0-9 和 _)
  • 变量名称区分大小写(age、Age 和 AGE 是三个不同的变量)

++请记住,变量名称区分大小写++

e.向多个变量赋值

Python 允许您在一行中为多个变量赋值:

python 复制代码
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

"""
输出
Orange
Banana
Cherry
"""
f.输出变量

如需结合文本和变量,Python 使用 + 字符:

字符+字符

python 复制代码
x = "awesome"
print("Python is " + x)



"""
输出
Python is awesome
"""

数字+数字

python 复制代码
x = 5
y = 10
print(x + y)


"""
输出
15
"""

不能字母+数字

错误实例

python 复制代码
x = 10
y = "Bill"
print(x + y)



"""
输出
Traceback (most recent call last):
  File "E:\py脚本\zxpy(自学python)_01.py", line 3, in <module>
    print(x + y)
          ~~^~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'
"""
g.全局变量

实例

在函数外部创建变量,并在函数内部使用它

python 复制代码
x = "awesome"

def myfunc():
  print("Python is " + x)
myfunc()

"""
输出
Python is awesome
"""
h.局部变量

在函数内部创建一个与全局变量同名的变量:

python 复制代码
x = "awesome"
def myfunc():
  x = "fantastic"
  print("Python is " + x)
myfunc()
print("Python is " + x)



"""
输出
Python is fantastic
Python is awesome
"""
j.global 关键字

通常,在函数内部创建变量时,该变量是局部变量,只能在该函数内部使用。

要在函数内部创建全局变量,您可以使用 global 关键字。

如果您用了 global 关键字,则该变量属于全局范围:

python 复制代码
x = "222111"

def myfunc():
  global x
  x = "fantastic"

myfunc()

print("Python is " + x)


"""
输出
Python is fantastic
"""

写博客总结:标题要从标题二开始,这样更加清楚明了

相关推荐
量子联盟27 分钟前
原创-基于 PHP 和 MySQL 的证书管理系统,免费开源
开发语言·mysql·php
知识分享小能手1 小时前
Vue3 学习教程,从入门到精通,使用 VSCode 开发 Vue3 的详细指南(3)
前端·javascript·vue.js·学习·前端框架·vue·vue3
pay4fun2 小时前
2048-控制台版本
c++·学习
时来天地皆同力.2 小时前
Java面试基础:概念
java·开发语言·jvm
豌豆花下猫2 小时前
让 Python 代码飙升330倍:从入门到精通的四种性能优化实践
后端·python·ai
夏末蝉未鸣012 小时前
python transformers库笔记(BertForTokenClassification类)
python·自然语言处理·transformer
hackchen2 小时前
Go与JS无缝协作:Goja引擎实战之错误处理最佳实践
开发语言·javascript·golang
知识分享小能手3 小时前
Bootstrap 5学习教程,从入门到精通,Bootstrap 5 表单验证语法知识点及案例代码(34)
前端·javascript·学习·typescript·bootstrap·html·css3
铲子Zzz3 小时前
Java使用接口AES进行加密+微信小程序接收解密
java·开发语言·微信小程序
小小小新人121234 小时前
C语言 ATM (4)
c语言·开发语言·算法