小白学python第一期

1.Python基础语法

1.1数据类型、运算符

1.1.1数字类型

import math

print(math.factorial(32))               # 计算32的阶乘
print(0.4-0.3 == 0.1)                   # 实数之间尽量避免直接比较大小

print(math.isclose(0.4-0.3, 0.1))       # 测试两个实数是否足够接近

a = 6**0.5

print(6**0.5**2 == 6)

c = 3+4j                                # Python内置支持复数及其运算
print(c+c)                              # 复数相加
print(c.real)                           # 查看复数的实部
print(c.imag)                           # 查看复数的虚部
print(3+4j.imag)                        # 相当于3+(4j).imag

1.1.2字符串类型

#===========================
#定义一个字符串类型text并给它赋值。
#===========================
text = '''Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.'''

print(len(text))                 # 字符串长度,即所有字符的数量
print(text.count('is'))          # 字符串中单词is出现的次数
print('Beautiful' in text)       # 测试字符串中是否包含单词beautiful
print('='*20)                    # 字符串重复
print('Good '+'Morning')         # 字符串连接

1.1.3列表元组字典集合

x_list = [1, 2, 3]    # 创建列表对象  

x_tuple = (1, 2, 3)   # 创建元组对象

x_dict = {'a':97, 'b':98, 'c':99}     # 创建字典对象,元素形式为“键:值”

x_set = {1, 2, 3}   # 创建集合对象

print(x_list[1])	# 使用下标访问列表中指定位置的元素,元素下标从0开始

print(x_tuple[1])	# 元组也支持使用序号作为下标,1表示第二个元素的下标

print(x_dict['a'])	# 访问字典中特定“键”对应的“值”,字典对象的下标是“键”

print(len(x_list))	# 查看列表长度,也就是其中元素的个数

print(x_tuple.index(2))		# 查看元素2在元组中首次出现的位置

#查看字典中哪些“键”对应的“值”为98 
for key, value in x_dict.items():
    if value == 98:
        print(key)
        
#查看集合中元素的最大值
print(max(x_set))

1.1.4算术运算符

#+运算符除了用于算术加法以外,还可以用于列表、元组、字符串的连接
print(3 + 5)
print(3.4 + 4.5)
print((3+4j) + (5+6j))
print('abc' + 'def')
print([1,2] + [3,4])
print((1,2) + (3,))
print("\n")

#-运算符除了用于整数、实数、复数之间的算术减法和相反数之外,还可以计算集合的差集。需要注意的是,在进行实数之间的运算时,有可能会出现误差。
print(7.9 - 4.5)                    # 注意,结果有误差
print(5 - 3)
num = 3
print(-num)
print(--num)                        # 注意,这里的--是两个负号,负负得正
print(-(-num))                      # 与上一行代码含义相同
print({1,2,3} - {3,4,5})            # 计算差集
print({3,4,5} - {1,2,3})
print("\n")

#*运算符除了表示整数、实数、复数之间的算术乘法,还可用于列表、元组、字符串这几个类型的对象与整数的乘法,表示序列元素的重复,生成新的列表、元组或字符串。
print(33333 * 55555)
print((3+4j) * (5+6j))
print('重要的事情说三遍!' * 3)
print([0] * 5)
print((0,) * 3)
print("\n")

#%运算符可以用于求余数运算,还可以用于字符串格式化。在计算余数时,结果与%右侧的运算数符号一致。
print(365 % 7)
print(365 % 2)
print('%c,%c, %c' % (65, 97, 48))   # 把65、97、48格式化为字符

1.1.5关系运算符

print(3+2 < 7+8)                       # 关系运算符优先级低于算术运算符,T
print(3 < 5 > 2)                       # 等价于3<5 and 5>2,T
print(3 == 3 < 5)                      # 等价于3==3 and 3<5T,T
print('12345' > '23456')               # 第一个字符'1'<'2',直接得出结论,F
print('abcd' > 'Abcd')                 # 第一个字符'a'>'A',直接得出结论,F
print([85, 92, 73, 84] < [91, 82, 73]) # 第一个数字85<91,直接得出结论,T
print([180, 90, 101] > [180, 90, 99])  # 前两个数字相等,第三个数字101>99,T
print({1, 2, 3, 4} > {3, 4, 5})        # 第一个集合不是第二个集合的超集,F
print({1, 2, 3, 4} <= {3, 4, 5})       # 第一个集合不是第二个集合的子集,F
print([1, 2, 3, 4] > [1, 2, 3])        # 前三个元素相等,并且第一个列表有多余的元素,T

1.1.6逻辑运算符

print(3 in range(5) and 'abc' in 'abcdefg')  #and表示与运算
print(3-3 or 5-2)	#or表示或运算
print(not 5)	#not表示非运算
print(not [])
print(2 and 3)

1.1.7身份运算符

a = 20
b = 20
 #is判断两个标识符是不是引用自同一个对象。
 #is not判断两个标识符是不是引用自不同对象。
 #==判断引用变量的值是否相同。
if ( a is b ):
   print ("1 : a 和 b 有相同的标识")
else:
   print ("1 :  a 和 b 没有相同的标识")
 
#修改变量 b 的值
b = 30
if ( a is b ):
   print ("3 : a 和 b 有相同的标识")
else:
   print ("3 : a 和 b 没有相同的标识")
 
#is 与 == 的区别
a = [1, 2, 3]
b = [1, 2, 3]
print(b == a)
print(b is a)

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐