Python 基础

1.输出 – 使用函数 print()
1)直接输出字符串

>>> print("hello")
hello

2)输出多个字符串,各个字符串中间用逗号隔开,此时会一次输出各个字符串,遇到逗号的地方会用输出一个空格

>>> print("I am", "a student")
I am a student

3)输出数值

>>> print(10)
10
>>> print(3.14)
3.14

4)输出表达式结果

>>> print(10+20)
30
>>> print(10 < 20)
True

2.输入 – 使用函数 input()
1)输入整数

>>> a = input()
10
>>> print(a)
10

输入 a = input() 之后,按回车再输入一个10, 此时 10 就会和变量 a 绑定,输入 a 的值,发现其为10。

输入浮点数

>>> a = input()
3.14
>>> print(a)
3.14

输入字符串

>>> s = input()
abce
>>> print(s)
abce

3.标识符

在 Python 中
1. 标识符是由 字符,数字,下划线(_)组成,但是不能以数字开头。
2. 是区分大小写的。
3. 以下划线开头的是有特殊意义的,所以在定义普通变量的时候最好不要使用下划线。

4.保留字

and assert break class continue def del elif else except exec finally
for from global if import in is lambda not or pass print raise return
while with yield

5.行和缩进
Python 与其他语言最大的区别就是,其代码块不是以大括号{}括起来,而是根据缩进来判断。
缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进数量。

[fanya@localhost code]$ vi test2.py
[fanya@localhost code]$ cat test2.py 
if 10 > 2:
	print("line1")
	print("line2")
		print("line3")
print("line4")
[fanya@localhost code]$ python3 test2.py 
  File "test2.py", line 4
    print("line3")
    ^
IndentationError: unexpected indent
上述代码由于 line3 未和其他行保持相同缩进
[fanya@localhost code]$ vi test2.py 
[fanya@localhost code]$ cat test2.py 
if 10 > 2:
	print("line1")
	print("line2")
	print("line3")
print("line4")
[fanya@localhost code]$ python3 test2.py 
line1
line2
line3
line4

6.注释
1.单行注释使用 #
2.多行注释使用三个单引号或三个双引号 括起来

[fanya@localhost code]$ cat test.py 
#这是一个单行注释
print("测试注释")
'''
这些是在多行注释中
不会显示
'''
print("多行注释")
[fanya@localhost code]$ python3 test.py 
测试注释
多行注释

7.多行语句
Python 的语句末尾一般不写分号,以一行的开始作为语句的结束,如果想将一条语句分多行书写,则可以使用折行符进行连接

>>> s = "i am
  File "<stdin>", line 1
    s = "i am
            ^
SyntaxError: EOL while scanning string literal


>>> s = "i am \
... a student"
>>> print(s)
i am a student

当语句中包含括号时,可以不使用折行符

>>> s = ['a','b'
... 'c','d']
>>> print(s)
['a', 'bc', 'd']

一般情况下是一行显示一条语句,如果想在一行显示多条语句,需要使用分号将语句分开

>>> print(10);print(20);print(30)
10
20
30
>>> print(10)print(20)
  File "<stdin>", line 1
    print(10)print(20)
                 ^
SyntaxError: invalid syntax
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值