【Python复健】Python入门基础知识整理1

主要从数据类型(数字 布尔 字符串)、容器(列表 字典 集合 元组)、函数、类四个方面整理了python的基础知识。


一、数据类型

1、数字类型(Numbers)

代表整数和浮点数,原理与其它语言相同。

x=5             # 直接定义数字
print(x)        # 输出"5"
print(x+1)      # 直接做运算 输出"6"
print(x**2)     # 两个**表示幂 即5^2=25 输出"25"
x += 1          # x = x+1
x *= 2          # x = x*2
## python的数字类型比较特殊,没有x+和x-运算符
print(type(x))  # 输出"<class 'int'>"

y = 5.5
print(type(y))  # 输出"<class 'float'>"

2、布尔类型(Booleans)

pyhton中使用的是英文单词而不是符号

T = True          # 直接定义
F = False
print(type(T))    # 输出"<class 'bool'>"
print(T and F)    # 与,输出"False"
print(T or F)     # 或,输出"True"
print(not F)      # 非,输出"True"
print(T != F)     # 输出"True"

3、字符串类型(Strings)

hello='hello'        
world = "world"     # 单双引号均可
print(hello)        # Prints "hello"
print(len(hello))   # 字符串长度;prints"5" 
hw=hello+world      # 字符串拼接直接使用+
print(hw)           # prints "hello world" 
hw12 ='%s %s %d' % (hello, world, 12) 
print(hw12)         # prints "hello world 12"

s = 'beautiful'
# 大小写变换
print(s.upper())         # prints"BEAUTIFUL"
print(s.capitalize())    # prints"Beautiful"

# 输出调整
print(len(s))            # prints"9"
print(s.rjust(11))       # prints"  beautiful"
print(s.center(11))      # prints" beautiful "
print(s.replace('iful','y'))   # prints"beauty"

print('  beauty  '.strip())    # prints"beauty"

二、容器

1、列表

列表其实就是Python中的数组,但是可以它可以动态的调整大小,并且可以包含不同类型的元素。

xs =
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值