【Python】基本语法知识

目录

一、print函数

二、变量类型

1.数字

2.字符串

3. 列表

4. 元组

5. 集合

6. 字典

三、循环

1. while循环

2. for循环

四、函数

五、类

1. 创建类

2. 实例化

3.继承


 

一、print函数

举例:

print a
print('这是一个%s'%(a))

二、变量类型

参考网站为:Python3 基本数据类型 | 菜鸟教程 (runoob.com)

Python3 中有六个标准的数据类型:

  • Number(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Set(集合)
  • Dictionary(字典)

Python3 的六个标准数据类型中:

  • 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);
  • 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。

1.数字

a, b, c, d = 20, 5.5, True, 4+3j

2.字符串

str = 'Runoob'

3. 列表

list = [ 'abcd', 786 , 2.23, 'runoob', 70.2 ]

4. 元组

tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )

5. 集合

sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu'}

6. 字典

数据为键值对

tinydict = {'name': 'runoob','code':1, 'site': 'www.runoob.com'}

三、循环

参考:Python3 循环语句 | 菜鸟教程 (runoob.com)

1. while循环

count = 0
while count < 5:
   print (count, " 小于 5")
   count = count + 1
else:
   print (count, " 大于或等于 5")

2. for循环

Python for 循环可以遍历任何可迭代对象,如一个列表或者一个字符串。

languages = ["C", "C++", "Perl", "Python"] 
for x in languages:
    print (x)

for i in range(5,9) :
    print(i)

四、函数

参考:Python3 函数 | 菜鸟教程 (runoob.com)

def area(width, height):
    return width * height

五、类

Python3 面向对象 | 菜鸟教程 (runoob.com)

1. 创建类

class Dog():
"""模拟小狗"""

    def_init_(self,name,age):
        """初始化"""
        self.name=name
        self.age=age

    def sit(self):
        """蹲下"""
        print(self.name.title()+"蹲下了")

    def roll_over(self):
        """打滚"""
        print("^")

2. 实例化

 Python3 面向对象 | 菜鸟教程 (runoob.com)

my_dog=Dog("while",6)

3.继承

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁荣生长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值