python基础学习之变量、运算符与数据类型

变量、运算符与数据类型

1. 注释

  • 在 Python 中,# 表示注释,作用于整行。

【例子】单行注释

# 这是一个注释
print("Hello world")

# Hello world
  • ''' ''' 或者 """ """ 表示区间注释,在三引号之间的所有内容被注释

【例子】多行注释

# 写下你的答案
"""
在代码块中打印(print)出 hello+你的姓名
"""
print("hello 张")
# hello 张

2. 运算符

算术运算符

操作符名称示例
+print(1 + 1)
-print(2 - 1)
*print(3 * 4)
/print(3 / 4)
//整除(地板除)print(3 // 4)
%取余print(3 % 4)
**print(2 ** 3)
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)
>=大于等于print(2 >= 4)
<小于print(1 < 2)
<=小于等于print(5 <= 2)
==等于print(3 == 4)
!=不等于print(3 != 5)
print(2 > 1)  # True
print(2 >= 4)  # False
print(1 < 2)  # True
print(5 <= 2)  # False
print(3 == 4)  # False
print(3 != 5)  # True

逻辑运算符

操作符名称示例
and(3 > 2) and (3 < 5)
or(1 > 3) or (9 < 2)
notnot (2 > 1)
print((3 > 2) and (3 < 5))  # True
print((1 > 3) or (9 < 2))  # False
print(not (2 > 1))  # False
print("**********************************")
print((3>2) and (5>1))

三元运算符

【例子】

x, y = 4, 5
if x < y:
    small = x
else:
    small = y

print(small)  # 4
print("************下面是三元运算符的写法*****************")
a = x if x<y else y
print(a)

其他运算符

注意:

  • is, is not 对比的是两个变量的内存地址
  • ==, != 对比的是两个变量的值
  • 比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。
  • 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。

运算符的优先级

运算符描述
**指数(最高优先级)
~+-按位翻转,一元加号和减号
* / % //乘,除,取模和取整除)
+ -加法减法
>> <<右移,左移运算符
&位‘AND’
^|位运算符
<=<>>=比较运算符
<>==!=等于运算符
=%=/=//=-=+==*=赋值运算符
is is not身份运算符
in not in成员运算符
not and or逻辑运算符
操作符名称示例
in存在'A' in ['A', 'B', 'C']
not in不存在'h' not in ['A', 'B', 'C']
is"hello" is "hello"
is not不是"hello" is not "hello"

3. 变量和赋值

  • 在使用变量之前,需要对其先赋值。
  • 变量名可以包括字母、数字、下划线、但变量名不能以数字开头。
  • Python 变量名是大小写敏感的,foo != Foo。
    title= "人生苦短,我爱python"
    print(title)  # 人生苦短,我爱python
    a = 1
    b = 2 
    print(a + b)  # 3

    4. 数据类型与转换

    类型名称示例
    int整型 <class 'int'>-876, 10
    float浮点型<class 'float'>3.149, 11.11
    bool布尔型<class 'bool'>True, False

    类型转换

  • 转换为整型 int(x, base=10)
  • 转换为字符串 str(object='')
  • 转换为浮点型 float(x)
  • a = 2
    print(a, type(a))
    # 2 <class 'int'>
    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

    Python 里面万物皆对象(object),整型也不例外,只要是对象,就有相应的属性 (attributes) 和方法(methods)。

    b = dir(int)
    print(b)

    5. print() 函数

  • import sys
    a = '我的人生  我做主'
    print(a, sep=' ', end='\n', file=sys.stdout, flush=False)
  • 将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按str()方式进行转换为字符串输出;
  • 关键字参数sep是实现分隔符,比如多个参数输出时想要输出中间的分隔字符;
  • 关键字参数end是输出结束时的字符,默认是换行符\n
  • 关键字参数file是定义流输出的文件,可以是标准的系统输出sys.stdout,也可以重定义为别的文件;
  • 关键字参数flush是立即把内容输出到流文件,不作缓存。

【例子】没有参数时,每次输出后都会换行。

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

【例子】每次输出结束都用end设置的参数&结尾,并没有默认换行。

shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'end='&''.")
for item in shoplist:
    print(item, end='&')
print('hello world')

# This is printed with 'end='&''.
# apple&mango&carrot&banana&hello world

【例子】item值与'another string'两个值之间用sep设置的参数&分割。由于end参数没有设置,因此默认是输出解释后换行,即end参数的默认值为\n

shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'sep='&''.")
for item in shoplist:
    print(item, 'another string', sep='&')

# This is printed with 'sep='&''.
# apple&another string
# mango&another string
# carrot&another string
# banana&another string

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值