程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics

編輯:Python
 Conditional expression :
if 1>2:print(111)
elif 2>3:print(222)
else:print(333)
for loop :
for i in range(3,10):
print(i)
Output results :for loop , In steps of 2 
for i in range(1,10,2):
print(i)

Output results :

If you want to print to one line ,print Add one more end Parameters , Such as :print(i,end="") that will do

 Print references :
name = " Wang Lufei "
s = f" I am a pirate {name}"
print(s)

result :

Reverse string

s = "123456"
print(s[::-1])

result :

Removes Spaces before and after the string

username = " admin "
realrm= username.strip();
print(realrm)
print(len(username))
print(len(realrm))

result :

  Modify variables :

"""
global: Local modification of global variables
nonlabal: Introduce an outer local variable locally
"""
a = 10
def func():
global a # Modifying global variables requires global
a = 20
func()
print(a)
def func1():
a = 10
def func2():
nonlocal a # Go up one level to find out if there is such a variable , Did not continue to look up a layer , Until the outermost layer ( Less than the overall situation )
a = 20
func2()
print(a)
func1()
result :20
20

  Array add remove elements :

list = []
list.append("aaa")
list.append("bbb")
list.insert(1,"111") # Insert the specified string at the specified position
print(list)
result = list.pop(1) # Remove subscript as 1 String
print(list) # Print removed list
print(result) # Print removed characters 

result :

Array loop :

list=["1a","2b","3c"]
for i in range(len(list)):# loop 0-list Subscript of length
print(i,end="")
print()
for item in list:# loop list The elements in
print(item,end="")

result : 

Dictionary value traversal :

dict = {"key":1,"key2":"11"}
print(dict.get("key"))
print(dict["key2"])
# Ergodic dictionary
for key in dict :# Traverse key, according to key take vaules
print(key,dict.get(key))
print(list(dict.keys()))# Get all the key
for key,values in dict.items():
print(key,values)

result : 

Element interchange position :

a = 10
b = 20
a,b = b,a
print(a)
print(b)
result : 20
10

  Determine the number of element occurrences :

lst = ["a","b","c","a","a"]
print(lst.count("a"))
result :3

Calculate from 0-100 And

print(sum(range(0,101)))
result :5050

Print 99 Multiplication table :

def write99():
for i in range(1, 10):
for j in range(1, i+1):
print(f"{i}*{j}={i*j}",end=" ")
print()
write99()

result :

 

Sort from small to large :

def bigToSimal(arr):
for i in range(1,len(arr)):
for j in range(0,len(arr)-i):
if arr[j] < arr[j+1]:
temp = arr[j+1]
arr[j+1] = arr[j]
arr[j] = temp
return arr
arra = [2, 5, 8, 1, 3]
print(bigToSimal(arra))
result :[8, 5, 3, 2, 1]

Decorator :

def guanjia(game):
def inner():
print(" Open technology ")
game()
print(" Turn off Technology ")
return inner
@guanjia
def play_hali():
print('hello,hali')
@guanjia
def play_ys():
print(" Towards the stars and the abyss ")
play_ys()
result : Open technology
Towards the stars and the abyss
Turn off Technology
# General decorator writing method
def wrapper(fn): #fn: Objective function
def inner(*args,**kwargs):
ret = fn(*args,**kwargs)
return ret
return inner


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved