phthon基础:变量、类型、判断及循环语句

phthon基础:变量、类型、判断及循环语句

1 变量、运算符和数据类型

1.1 注释

单行注释: # , 与C/C++的“ // ” 作用相同, 与sh脚本、makefile的注释一样

多行注释:’’’ ‘’’ 三个引号间的内容将被注释,与C/C++的 “ /**/ ” 作用相同

#这是单行注释

'''
这是多行注释
第一行
第二行
'''

基本数据类型有

  • int 整形
  • float 浮点型
  • str 字符串类型
  • bool 布尔型

1.2 运算符

1.2.1算数运算符

操作符名称示例
+1 + 1
-2 - 1
*3 * 4
/3 / 4
//整除(地板除)3 // 4
%取余3 % 4
**2 ** 3

关于整除,在C/C++中, 如果是两个整形除并赋值给一个整形变量,就是整除,但在Python中要使用 “//” 运算符,这是有区别的地方。还有幂运算 ,这是在C/C++中没有的。

【例子】

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(4 / 2) #2.0   即使可以整除也会转为浮点数!

1.2.2 比较运算符

操作符名称示例
>大于2 > 1
>=大于等于2 >= 4
<小于1 < 2
<=小于等于5 <= 2
==等于3 == 4
!=不等于3 != 5

这和C/C++完全一样的。但是python 的变量是自动类型的,那么 100 和 “100”能够比较吗,结果是怎样的。看下面的例子

在这里插入图片描述

可见解释器还是会做类型判断的,不同类型的变量不能比较。

1.2.3 逻辑运算符

操作符名称示例C/C++
and(3 > 2) and (3 < 5)&&
or(1 > 3) or (9 < 2)||
notnot (2 > 1)!

1.2.4 位运算符

操作符名称示例C/C++
~按位取反~4~
&按位与4 & 5&
|按位或||
^按位异或4 ^ 5^
<<左移4 << 2<<
>>右移4 >> 2>>

”与“和”按位与“的区别:与运算输出真假,按位与输出数值。但是大多情况下效果一样

(1 && 0) 和 (1 & 0) 		#条件判断都为真
(1 && 0 && 1) 			#1 && 0为真,不再判断 && 1
(1 & 0 & 1)				# 会一直运算完整个表达式,得到结果为0,判为假。

可见做逻辑运算时,使用 && 效率更高

例子】有关二进制的运算,参见“位运算”部分的讲解。

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

1.2.5 三元运算符

【例子】

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

print(small)  # 4

有了这个三元操作符的条件表达式,你可以使用一条语句来完成以上的条件判断和赋值操作。

【例子】

x, y = 3, 4
print(x,y)
small = x if x > y else y#与C三目运算符类似: int a = 1 > 2 ? 3 : 4;
print(small)

输出:3, 4

​ 4

1.2.6其他运算符**

操作符名称示例
in存在'A' in ['A', 'B', 'C']
not in不存在'h' not in ['A', 'B', 'C']
is"hello" is "hello"
not is不是"hello" is not "hello"

【例子】

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
  • is, is not 对比的是两个变量的内存地址

  • ==, != 对比的是两个变量的值

  • 比较的两个变量,指向的都是地址不可变的类型(str等),那么is,is not 和 ==,!= 是完全等价的。

  • 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的

【例子】比较的两个变量均指向不可变类型。

a = "hello"
b = "hello"
print(a is b, a == b)  # True True
print(a is not b, a != b)  # False False

​ True True

​ False False

【例子】比较的两个变量均指向可变类型

a = ["hello"]
b = ["hello"]
print(a is b, a == b)  # False True
print(a is not b, a != b)  # True False

​ False True

​ True False

1.2.7 运算符优先级

​ 太多了不用记,记不住的时候多使用 括号()就好了,(〃‘▽’〃)

3 变量赋值

使用 ”=“ 赋值,没什么注意的,都和C语言一样。

4数据类型装换

语法: 类型(要转换的变量)

和C语言一样,没什么好说的,但注意一点用bool转换的是非空容器变量时为true

5 print 函数

print函数和C语言还是不一样的,语法如下

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

【例子1】

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

[例子2]

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

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

6 位运算

位运算和C语言也是一模一样的。偷懒就不敲了

7 if 判断语句

语法:

if condition:				
	code					


if condition:
	code
else:

上面的例子,没有使用{ }来区分代码块,那么if 语句的作用域到哪里为止呢? 答案是通过缩进来区分哪些语句是属于if 的作用域

if condition:				
	code1
    code2
    code3			//如果 condition成立 ,有缩进的code1、2、3都会执行

code 4				/无论 condition成立 ,code4都会执行,因为它不在if语句的作用域内

7.1 if语句的嵌套

hi = 6
if hi > 2:
    if hi > 7:
        print('好棒!好棒!')
else:
    print('切~')

# 无输出

7.3 if -elif-else 语句

if condition1:
    print('A')
elif condition1:
    print('B')
elif condition1:
    print('C')
else:
    print(error')

8 循环语句

8.1 for 和 while

循环语句有for 和 while两种。和C语言的唯一区别在于,可以和else搭配使用

例如下面的例子

while expression:
	code1
	code2
else
	code3

​ 如果while正常结束,则else会被执行,如果是被break结束,else则不会执行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值