Python入门(代码示例)

本文介绍了Python的基本语法,包括打印输出、注释、代码缩进和多行语句。接着讲解了变量的使用,如点对点赋值和多对多赋值。然后详细阐述了常用的数据类型,如数字、字符串、列表、元组和字典,以及它们的操作。文章还涵盖了算术运算符、比较运算符、布尔运算符等基础运算,并展示了条件判断语句和循环语句的使用。此外,还介绍了函数的定义和参数传递,以及类的创建、继承和重写。
摘要由CSDN通过智能技术生成

python入门

1.基本语法

  • 打印输出
  • 注释
  • 代码的缩进
  • 多行语句的分割
#1打印输出
print("Hello,World")
print("-"*10)
print("Hello")
print("World")
print("-"*10)
print("Hello","World")
#2注释
"""
多行注释,三个双引号
"""
'''
多行注释
'''
print("hello")
#3控制缩进
for i in range(3):
    print(i)
    print("-"*10)
#4语句的分割,用斜杠\
print("Hello,World.Hello,World.\
Hello,World.Hello,world.")

2.python变量

#点对点赋值
int_num=10
float_num=10.00
string="Hello,World"

print(int_num)
print(float_num)
print(string)
#点多多,多对多
string1 = string2 = string3 = "Hello,World"
string4, string5,string6="Hello","World","Hello,World"

print(string1)
print(string2)
print(string3)
print(string4)
print(string5)
print(string6)

3.常用的数据类型

  • 数字:int,float
  • 字符串
  • 列表[ ]
    • 容器型数据类型
  • 元组( )
    • 容器型数据类型
    • 不能重新赋值
    • 只读型列表
  • 字典{ }
    • 容器型数据类型
    • 有更灵活的操作和复杂的性质
    • 无序的元素集合
    • 用元素对应的键值来赋值
#自定义浮点数打印输出的精度
float_num=10.000
print(float_num)
print("%f" % float_num)
print("%.2f" % float_num)
print("%.4f" % float_num)
string1 = 'Hello World_@1'
string2 = "Hello,World_@2"
print(string1,string2)
#前索引值、后索引值
string = "Hello,World"
string0 = string[0]
string1 = string[0:13]
string2 = string[0:5]
string3 = string[-1]
string4 = string[-5:]
string5 = string[6:]
string6 = string[:5]

print(string0)
print(string1)
print(string2)
print(string3)
print(string4)
print(string5)
print(string6)
list1 = ["Hello,World", 100 , 10.00]
list2 = [123,'Hi']

print(list1)
print(list1[0])
print(list1[1:])
print(list1[-1])
print(list1+list2)
list_old = ["Hello,World",100 , 10.00]
print(list_old)

list_old[0] = "Hello,Wrld,Hi"
list_new = list_old
print(list_new)
tuple1 = ("Hello,World" , 100 , 10.00)
tuple2 = (123, "Hi")
print(tuple1)
print(tuple1[0])
print(tuple1[1:])
print(tuple1[-1])
print(tuple1 +tuple2)
dict_1={}
dict_1["one"] = "This is one"
dict_1[2] ="This is two"   #设置key为2的value
dict_info = {"name":"Tang","num":7272,"city":"GL"}

print(dict_1["one"])
print(dict_1[2])  #键值为2
print(dict_1)
print(dict_info)
print(dict_info.keys())
print(dict_info.values())

4.python运算

  1. 算数运算符

    • %取模运算
    • ** 求幂运算
    • // 取整运算
  2. 比较运算符

    • 返回布尔值(true/false)
  3. 布尔运算符

    • 与 and
    • 或 or
    • 非 not
  4. 成员运算符

    • 返回值是布尔型
    • in 判断是否是目标中的元素
  5. 身份运算符

    • 返回值是布尔型
    • is/is not
    • 判断比较的变量是否是同一个对象,或者定义的变量是否指向相同的内存地址
a = 5
b = 2
c = a//b
print("a//b=",c)
c= a % b
print("a%b=",c)
c=a**b
print("a**b=",c)#5^2
c=b**a
print("b**a=",c)#2^5
#不需要对运算后结果进行存储的话,简化运算代码
a = 5
b = 2
a += b
print("a+b=",a)

a = 5
a *= b
print("a*b=",a)

a = 5
a //=b
print("a//b=",a)
#单层比较
a = 5
b = 2
print(a == b)
print(a != b)
print(a > b)
print(a >= b)
#多层比较
a = 5
b = 2
c = 4
print(a == b == c)
print(a > b > c)
print(a < b < c)
#布尔运算
a = True
b = False

print(a and b)
print(a or b)
print(not a)
a = 2
b = 1
c = 3

print(a>b and a<c)
print(a>b or a<b)
print(not a<b)
print(a>b and c<b and c>a)
print(a>b and a<b or c>a)
list_1 = ["I","am","super","man"]
a = "super"
b = 1

print(a in list_1)
print(b in list_1)
#身份运算符实例
a = 500
b = 500
print("a的内存地址:",id(a))
print("b的内存地址:",id(b))
print("a is b",a is b)
print("a is not b",a is not b)
print("a == b",a == b)

a = 10
b = 10
print("a的内存地址:",id(a))
print("b的内存地址:",id(b))
print("a is b",a is b)
print("a is not b",a is not b)
print("a == b",a == b)

5.Python条件判断语句

6.Python循环语句

  • break 中断当此循环并结束整个循环语句
  • continue 中断当次循环并直接开始下次循环
  • pass 不做任何操作,继续执行当次循环中的后续代码,主要用于保持代码块的完整性
#判断2
number = 10
if number == 10:
    print("The number is equeal",number)
else:
    print("The number is not equal 10.")
#判断4
number = 15
if number == 10:
    print("The number is equal",number)
elif number >10:
    print("The number is greater than 10.")
elif number < 10:
    print("The number is less than 10.")
else:
    print("Error.")
number = 0
while (number < 10):
    print("The number is",number)
    number +=1
number = 10
for i in range(10):
    if i<number:
        print("The number is", i)
#break控制
number = 10
for i in range(10):
    if i == 5:
        break
    if i<number:
        print("The number is",i)
#continue控制
number = 10
for i in range(10):
    if i == 5:
        continue
    if i<number:
        print("The number is",i)
#continue控制
number = 10
for i in range(10):
    if i == 5:
        pass
    if i<number:
        print("The number is",i)
a_list = [1,2,3,4,5,6]

for i in a_list:
    print("The number is ",i)

7.Python中的函数

  • 定义函数
  • 函数的参数
    • 必备参数
    • 关键字参数
    • 默认参数
    • 不定长参数
#不进行参数传递的函数
def function():
    print("Hello, World")
    return

a = function()
print(a)
#进行参数传递
def function(string = "Hi"):
    print("What you say is:",string)
    return

function()
function(string = "Hello,World.")
def function1(string):#必备参数
    print("What you say is:",string)
    return

def function2(string = "Hi"):#默认参数
    print("What you say is:",string)
    return

def function3(string2,string1):#关键字参数
    print("What you say is:",string1,string2)
    return

def function4(arg1,*arg2):#不定长参数
    print(arg1)
    for i in arg2:
        print(i)
    return

function1("Hello,world")
function2()
function3("world","hello")
function4(10,1,2,30,4)

8.Python中的类

  • 类的创建
    • 类变量
    • init()类的初始化方法
    • self类的实例,定义类的方法时必须要有,调用时不必传入的参数
  • 类的继承
    • 类初始化方法不会被自动调用,需要在子类中重新定义类的初始化方法
  • 类的重写
#类的定义
class Student:
    
    student_Count = 0
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        Student.student_Count =Student.student_Count + 1
        
    def dis_student(self):
        print("Student name:",self.name,"Student age:",self.age)
        
student1 = Student("Tang","20")
student2 = Student("Wu","22")

student1.dis_student()
student2.dis_student()
print("Total Student:",Student.student_Count)
#类的继承
class People:
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def dis_name(self):
        print("name is:",self.name)
        
    def set_age(self,age):
        self.age = age
        
    def dis_age(self):
        print("age is:",self.age)
        
class Student(People):
    def __init__(self, name, age, school_name):
        self.name = name
        self.age = age
        self.school_name = school_name
        
    def dis_student(self):
        print("school name is:",self.school_name)
        
student = Student("Wu","20","XUEXIAO")
student.dis_student()#调用子类的方法
#调用父类的方法
student.dis_name()
student.dis_age()
student.set_age(22)
student.dis_age()
#类重写
class Parent:
    
    def __init__(self):
        pass
    def print_info(self):
        print("This is Parent.")
class Child(Parent):
    
    def __init__(self):
        pass
    
    def print_info(self):
        print("This is Child.")
        
child = Child()
child.print_info()

参考书目:《深度学习之PyTorch实战计算机视觉》唐进民

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值