python赋值01_Python 01入门,python

python入门01

1.注释

单行注释:# 注释内容

多行注释:""" 多行注释 “”" ‘’’ 多行注释 ‘’’

2.运算符

(1)算术运算符

36cef2c1c5c74c216201a88bccc1b9eb.png

(2)比较运算符

f0762bdeca834e888c7f1b69832ed0ce.png

(3)逻辑运算符

9a34468251121842ff46ce7b78a41a87.png

(4)位运算符

3c795cb98ce77b42d5fd3ccb0dd38175.png

(5)其他运算符

69eb26216b09146712226aed39008d54.png

运算符的优先级

一元运算符优于二元运算符。例如3 ** -2等价于3 ** (-2)。

先算术运算,后移位运算,最后位运算。例如 1 << 3 + 2 & 7等价于 (1 << (3 + 2)) & 7。

逻辑运算最后结合。例如3 < 4 and 4 < 5等价于(3 < 4) and (4 < 5)。

3.变量和赋值

在使用变量之前,需要对其先赋值。

变量名可以包括字母、数字、下划线、但变量名不能以数字开头。

Python 变量名是大小写敏感的,foo != Foo。

4.数据类型和转换

3fde835f562556bb2f7bdc88eb25198d.png

5.print()函数

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按str()方式进行转换为字符串输出;

关键字参数sep是实现分隔符,比如多个参数输出时想要输出中间的分隔字符;

关键字参数end是输出结束时的字符,默认是换行符\n;

关键字参数file是定义流输出的文件,可以是标准的系统输出sys.stdout,也可以重定义为别的文件;

关键字参数flush是立即把内容输出到流文件,不作缓存。

代码练习

# 这是一个注释

print("Hello world")

# Hello world

'''

这是多行注释,用三个单引号

这是多行注释,用三个单引号

这是多行注释,用三个单引号

'''

print("Hello china")

# Hello china

"""

这是多行注释,用三个双引号

这是多行注释,用三个双引号

这是多行注释,用三个双引号

"""

print("hello china")

# hello china

print(1 + 1) # 2

print(2 - 1) # 1

print(3 * 4) # 12

print(3 / 4) # 0.75

print(3 // 4) # 0

print(3 % 4) # 3

print(2 ** 3) # 8

print(2 > 1) # True

print(2 >= 4) # False

print(1 < 2) # True

print(5 <= 2) # False

print(3 == 4) # False

print(3 != 5) # True

print((3 > 2) and (3 < 5)) # True

print((1 > 3) or (9 < 2)) # False

print(not (2 > 1)) # False

print(bin(4)) # 0b100

print(bin(5)) # 0b101

print(bin(~4), ~4) # -0b101 -5

print(bin(4 & 5), 4 & 5) # 0b100 4

print(bin(4 | 5), 4 | 5) # 0b101 5

print(bin(4 ^ 5), 4 ^ 5) # 0b1 1

print(bin(4 << 2), 4 << 2) # 0b10000 16

print(bin(4 >> 2), 4 >> 2) # 0b1 1

letters = ['A', 'B', 'C']

if 'A' in letters:

print('A' + ' exists')

if 'h' not in letters:

print('h' + ' not exists')

# A exists

# h not exists

print(-3 ** 2) # -9

print(3 ** -2) # 0.1111111111111111

print(1 << 3 + 2 & 7) # 0

print(-3 * 2 + 5 / -2 - 4) # -12.5

print(3 < 4 and 4 < 5) # True

myTeacher = "老马的程序人生"

yourTeacher = "小马的程序人生"

ourTeacher = myTeacher + ',' + yourTeacher

print(ourTeacher) # 老马的程序人生,小马的程序人生

print(int('520')) # 520

print(int(520.52)) # 520

print(float('520.52')) # 520.52

print(float(520)) # 520.0

print(str(10 + 10)) # 20

print(str(10.1 + 5.2)) # 15.3

shoplist = ['apple', 'mango', 'carrot', 'banana']

print("This is printed without 'end'and 'sep'.")

for item in shoplist:

print(item)

# This is printed without 'end'and 'sep'.

# apple

# mango

# carrot

# banana

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值