Python基础知识(一)

一、基础类型

  • 01、入门的第一步
print('Hello World!')	# Hello World!
  • 02、字符串的输出
# 字符串可以被三种符号包围-',","""
print('Hello World!')	# Hello World!
print("Hello World!")	# Hello World!
print("""Hello World!""")	# Hello World!
  • 03、\t(制表符,即Tab)、\n(换行)
# \t
print("Hello \t World!")	# Hello 	 World!
# \n
print("Hello \nWorld!")		# Hello 
							# World!
  • 04、‘\’ 转义字符
print("I\'m a boy")	# I'm a boy
  • 05、打印变量
# 创建一个变量,用该变量存储要用到的信息
note = "Hello World!"
print(note)		# Hello World!

二、字符串操作

字符串是python的特殊类型 ,字符串类在python中默认可用 ,无需import语句导入

  • 01、字符串的转换大小写
note = "Hello World!"
# 将英文字母全部转换为小写
print(note.lower())	# hello world!
# 将英文字母全部转换为大写
print(note.upper())	# HELLO WORLD!
  • 02、返回"标题化"的字符串
# 所有单词都是以大写开始,其余字母均为小写
note = "hello world!"
print(note.title()) # Hello World!
  • 03、查询方法含义
# 查询lower()的含义
note = "hello world!"
help(note.lower)	""" lower() method of builtins.str instance 
					Return a copy of the string converted to lowercase."""
  • 04、拆分字符串
# split() 用于字符串拆分
note = "Hello World!"
list = note.split(' ')
print(list)	# ['Hello', 'World!']
  • 05、拼接字符串
print("0" + "1")	# 01
print("Hel" + "lo")	# Hello
  • 06、重复输出字符串
note = "Hello"
print(note * 3)	# HelloHelloHello
  • 07、替换字符串的值
# replace(old, new[, max]) ,不会改变原来字符串的内容
# old -- 将被替换的子字符串
# new -- 新字符串,用于替换old子字符串
# max -- 可选字符串, 替换不超过 max 次
note = "this is a example,this is really string!"
print(note.replace("is","was"))		# thwas was a example,thwas was really string!
print(note.replace("is","was",3))	# thwas was a example,thwas is really string!

三、基础运算

  • Python有4中数字类型(Number):
    • int(有符号整型 )
    • long(长整型[也可以代表八进制和十六进制],数字后面建议使用大写L)Python3.X版本已移除long类型,用int替代
    • float(浮点型)
    • complex(复数),复数由实数部分和虚数部分构成,可以用 a + bj,或者 complex(a,b) 表示, 复数的实部 a 和虚部 b 都是浮点型
  • 数字实例
    • int:10、100、-456、0x260
    • long:51924361L 、-0x19323L
    • float:0.0、-21.9
    • complex:3.14j、45.j
  • 数学运算
  • 01、加法
print(1+1)	# 2
  • 02、减法
# 若一个操作数为float,则结果也为float
print(130-2.0)	#	128.0
  • 03、除法
# 双int操作数相除,结果为float
print(130/2)	# 65.0
# 若一个操作数为float,则结果也为float
print(130.0/2)	# 65.0
  • 04、整除
print(9 // 2)	# 4
print(9.0 // 2)		# 4.0
  • 05、乘法
print(2*3)	# 6
print(2*3.0)	# 6.0
  • 06、乘方
print(2**3)	# 8
print(2.0**3)	#8.0
  • 07、取余
print(9%2)	# 1
print(9.0%2)	# 1.0

四、if语句

  • 比较操作符:>、>=、<、<=、==、!=、<>(python3.x已废弃)
逻辑操作符描述
and如果两个操作数均为True,则condition变为True.
or如果两个操作数中的任何一个为True,则condition变为True.
not用于反转逻辑(不是False变为True,而不是True变为False
num = 5
if num > 0 and num < 6:
    print(num)	# 5
num = 5
if num > 0 or num > 6:
    print(num)	# 5
num = 5
print(not num >6)	# True

五、else 语句

  • 必须在if或elif语句之后。最多可以有一个else语句。仅当上面的所有“if”和“elif”语句都为False时才会执行
num = 1
if num > 3 :
    print("Hi")
else: 
    print("number is not greater than 3")	
# 输出:number is not greater than 3

六、elif 语句

  • 必须在if语句之后。elif语句语句允许您检查True的多个表达式,并在其中一个条件求值为True时立即执行代码块 。
  • 与else类似,elif语句是可选的 。但是elif语句可以有多个。
num = 21
if num > 50:
    print('num is larger than 50')
elif num == 21:
    print('num = 21')
else:
    print('Catchall condition')
# 输出:num = 21

七、列表(List)

# 列表的初始化
list = [1,5,3,4,3]
# 访问列表里面的值
print(list[0])	# 1,从列表的首端开始,0、1、2...
print(list[-1])	# 3,从列表的末尾开始,-1、-2、-3...
# 切分列表
print(list[0:2])	# [1, 5],左闭右开,从0开始,到1,不包括2
print(list[:3])		# [1, 5, 3] 默认从0开始
print(list[1:])		# [5, 3, 4, 3] 默认访问到末尾
# 取列表的最大值, 最小值, 长度, 以及总和
print(max(list),min(list),len(list),sum(list))		# 5 1 5 16
# 对列表中对象出现次数进行统计
print(list.count(3))		# 2
# 列表的初始化
list = [4, 1, 5, 4, 10, 4]
# 从列表中找出某个值第一次出现的索引位置
print(list.index(5))	# 2
# 指定从某个索引开始搜索某个值出现的位置,index(value,start)
print(list.index(4,2))	# 3
# 指定从某个索引开始,某个索引结束,搜索某个值出现的位置,index(value,[start,stop)),遵循左闭右开
print(list.index(4,5,6))	# 5
# 对列表进行升序排序,列表已发生改变
x = [3, 7, 2, 11, 8, 10, 4]
x.sort()
print(x)	# [2, 3, 4, 7, 8, 10, 11]
# 对列表进行降序排序,列表已发生改变
x.sort(reverse = True)
print(x)	# [11, 10, 8, 7, 4, 3, 2]


# 对列表进行升序排序,列表未发生改变
x = [3, 7, 2, 11, 8, 10, 4]
y = sorted(x)
print(y)	# [2, 3, 4, 7, 8, 10, 11]
print(x)	# [3, 7, 2, 11, 8, 10, 4]
# 在列表结尾添加一个对象
x = [3, 7, 2, 11, 8, 10, 4]
x.append(5)
print(x)	# [3, 7, 2, 11, 8, 10, 4, 5]
# 删除列表中一个对象
x.remove(2)
print(x)	# [3, 7, 11, 8, 10, 4]
# 删除列表中指定位置的对象,可以返回删除的对象的值
print(x.pop(1))	# 7
print(x)	# [3, 2, 11, 8, 10, 4]
# 合并列表,可以合并两个拥有不同数据类型的列表
x = [3, 7, 2, 11, 8, 10, 4]
y = ['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']
print('x+y=',x+y)	
# x+y= [3, 7, 2, 11, 8, 10, 4, 'Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']

# 通过在末尾续加的方式来延长列表
x = [3, 7, 2, 11, 8, 10, 4]
x.extend([1,2,3])
print(x)	# [3, 7, 2, 11, 8, 10, 4, 1, 2, 3]
# 在列表指定位置前插入对象
x = [3, 7, 2, 11, 8, 10, 4]
x.insert(2,[1,2])
print(x)	# [3, 7, [1, 2], 2, 11, 8, 10, 4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值