栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python基本语法快速入门

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python基本语法快速入门

Python3x官方文档:Python 标准库 — Python 3.10.0 文档https://docs.python.org/zh-cn/3/library/index.html

注释:
#单行注释

​​​​​​​'''
多行注释
'''

变量:
# int float complex
value = 1
print(value)
print(value+1)

执行结果:
1
2
-------------------------------

import math
a = -1
print(abs(a))#取绝对值
b = 1.8
print(round(b))#四舍五入
print(pow(a,2))
#ceil:大于一个数的最小整数
#floor:小于一个数的最整数
print(math.ceil(b))
print(math.floor(b))
执行结果:
1
2
1
2
1

字符串:
​​​​​
string = "俺是n字符串"
print(string)

执行结果:
俺是
字符串
-----------------------------------

string1="hello"
string2="world"
print(string1+string2)
print(string1[0])
print(string1[0:4])

执行结果:
helloworld
h
hell
------------------------------------

string = "helloworld"
#字符串的一些函数:
# len():或取字符串长度
print(len(string))
# capitalize:字符串首字母大写
print(string.capitalize())
# upper:字母变大写
print(string.upper())
#lower:字母变小写
print(string.lower())
#find:查找字符串
print(string.find("wor"))

执行结果:
10
Helloworld
HELLOWORLD
helloworld
5

列表: 
list =[1,2,"hello",3,4,5]
print(list[2])
list.append(6)
list.reverse()
list.remove(2)
print(list)

执行结果:
hello
[6, 5, 4, 3, 'hello', 1]
元组: 
tuple1 = (1,2,3)  # 元组不可修改
print(list(tuple1)) # 元组转换为列表

执行结果:
[1, 2, 3]
 条件控制:
score = int(input("Please input:"))
if (score>=90):
    print("你好厉害!")
elif(90>score>=70):
    print("你不太行了!")
else:
    print("你啥也不是了!")
    

执行结果:
Please input: 72
你不太行了!
 循环:
a=10
while (a>5):
    print(a)
    a=a-1

执行结果:
10
9
8
7
6
------------------------

string ="你好"
for char in string:
    print(char)

执行结果:
你
好
-------------------------
   
list = [1,2,3]
for i in range(0,len(list),1):
    print(list[i])

执行结果:
1
2
3
----------------------------
for i in range(10):
    print(i)
    if(i==3):
      break
执行结果:
0
1
2
3

函数:
def get_sum(num1,num2):
    result=num1+num2
    return result
a=1
b=2
print(get_sum(a,b))

执行结果:
3
 类:
class Person:
    def __init__(self,name,height):  #构造函数
        self.name = name
        self.height = height
        
    def showname(self): 
        print("My name is "+self.name)
        
    def showheight(self):
        print("My height is "+str(self.name))
        
person = Person("艳艳",165)
person.showname()
person.showheight()

执行结果:
My name is 艳艳
My height is 艳艳        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/329262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号