python基础知识总结

变量(Variables)

我们使用变量来暂时存储数值
price = 10
rating = 4.9
name = ‘myfirstwork’
is_published = True
其中,

  • price是整数
  • rateing是一个浮点数
  • name是一个字符串
  • is_published是一个boolean,True or False

实例变量:是在类声明的内部但是在类的其他成员方法之外声明的。它在整个类中都可以调用,前面一般加一个_表示实例变量。
类变量:类变量指的是在类中,但在各个类方法外定义的变量。类变量的特点是,所有类的实例化对象都同时共享类变量,也就是说,类变量在所有实例化对象中是作为公用资源存在的。类方法的调用方式有 2 种,既可以使用类名直接调用,也可以使用类的实例化对象调用。

#使用类名直接调用
print(CLanguage.name)
print(CLanguage.add)
#修改类变量的值
CLanguage.name = "Python教程"
CLanguage.add = "http://c.biancheng.net/python"
print(CLanguage.name)
print(CLanguage.add)

在这里插入图片描述

注释Comment

使用#添加单行注释
#你是一蜘蛛b
使用‘’‘ ’‘’添加多行注释
‘’‘这是
一个
多行注释’‘’

输入和输出(input and output)

birth_year = int (input(‘Birth year:’))

注意:input()总是把输入的内容转换成字符串,所以我们在之前加int()进行强制转换

print(birth_year)

格式化输入和输出

a = input().upper() #会把所有输入字符转换成大写
a = input().lower() #会把所有输入字符转换成小写

print(birth_year.upper()) # 把所有字符中的小写字母转换成大写字母
print(birth_year.lower()) # 把所有字符中的大写字母转换成小写字母
print(birth_year.capitalize()) # 把第一个字母转化为大写字母,其余小写
print(birth_year.title()) # 把每个单词的第一个字母转化为大写,其余小写

print(birth_year){10:.2f}
输出保留两位小数,并且之上占十个字符串

字符串(string)

可以用单引号或者双引号来定义字符串,

可以使用[] 获取字符串中的单个字符

course = ‘Python for Beginners’
course[0]   # returns the first charactercharacter
course[1]   # returns the second 
charactercourse[-1]  # returns the first character from the end`
name = ‘Mosh’ message = f’Hi, my name is {
   name}’message.upper()   # to convert to uppercase
message.lower()   # to convert to lowercase
message.title()   # to capitalize the first letter of every word
message.find(‘p’) # returns the index of the first occurrence of p   (or -1 if not found) message.replace(‘p’, ‘q’)

列表(List)

numbers = [1, 2, 3, 4, 5]
numbers[0]           # returns the first item 
numbers[1]           # returns the second item
numbers[-1]          # returns the first item from the end
numbers[-2]          # returns the second item from the end

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值