\>>># example 1
\>>> user ={'username':'Fan','age':20,'mail\_id':'codemaker2022@qq.com','phone':'18650886088'}
\>>>print(user){'mail\_id':'codemaker2022@qq.com','age':20,'username':'Fan','phone':'18650886088'}
\>>>print(user\['age'\])20
\>>>for key in user.keys():
\>>>print(key)
mail\_id
age
username
phone
\>>>for value in user.values():
\>>>print(value)
codemaker2022@qq.com
20
Fan
18650886088
\>>>for item in user.items():
\>>>print(item)('mail\_id','codemaker2022@qq.com')('age',20)('username','Fan')('phone','18650886088')
\>>># example 2
\>>> user ={
\>>>'username':"Fan",
\>>>'social\_media': \[
\>>>{
\>>>'name':"Linkedin",
\>>>'url':"https://www.linkedin.com/in/codemaker2022"
\>>>},
\>>>{
\>>>'name':"Github",
\>>>'url':"https://github.com/codemaker2022"
\>>>},
\>>>{
\>>>'name':"QQ",
\>>>'url':"https://codemaker2022.qq.com"
\>>>}
\>>> \],
\>>>'contact': \[
\>>>{
\>>>'mail': \[
\>>>"mail.Fan@sina.com",
\>>>"codemaker2022@qq.com"
\>>> \],
\>>>'phone':"18650886088"
\>>>}
\>>> \]
\>>>}
\>>>print(user){'username':'Fan','social\_media': \[{'url':'https://www.linkedin.com/in/codemaker2022','name':'Linkedin'},{'url':'https://github.com/codemaker2022','name':'Github'},{'url':'https://codemaker2022.qq.com','name':'QQ'}\],'contact': \[{'phone':'18650886088','mail': \['mail.Fan@sina.com','codemaker2022@qq.com'\]}\]}
\>>>print(user\['social\_media'\]\[0\]\['url'\])
https://www.linkedin.com/in/codemaker2022
\>>>print(user\['contact'\])
\[{'phone':'18650886088','mail': \['mail.Fan@sina.com','codemaker2022@qq.com'\]}\]
6.注释
单行注释,以井字符(#)开头,后面带有消息并在行尾结束。
# 定义用户年龄
age =27
dob ='16/12/1994'# 定义用户生日
多行注释,用特殊引号(“”")括起来,你可以将消息放在多行中
Python小常识
This is a multi line comment
7.基本功能
print()函数在控制台中打印提供的消息。此外你还可以提供文件或缓冲区输入作为在屏幕上打印的参数
print(object(s), sep=separator, end=end,file=file, flush=flush)print("Hello World")# prints Hello World print("Hello","World")# prints Hello World?
x =("AA","BB","CC")print(x)# prints ('AA', 'BB', 'CC') print("Hello","World", sep="---")# prints Hello---World
\>>> num =5
\>>>if(num >0):
\>>>print("Positive integer")
\>>>else:
\>>>print("Negative integer")
elif语句。
\>>> name ='admin'
\>>>if name =='User1':
\>>>print('Only read access')
\>>>elif name =='admin':
\>>>print('Having read and write access')
\>>>else:
\>>>print('Invalid user')
Having read and write access
9.循环语句
循环是一个条件语句,用于重复某些语句(在其主体中),直到满足某个条件。
在Python中,我们通常使用for和while循环。
for循环。
\>>># loop through a list
\>>> companies = \["apple","google","tcs"\]
\>>>for x in companies:
\>>>print(x)
apple
google
tcs
\>>># loop through string
\>>>for x in"TCS":
\>>>print(x)
T
C
S
\>>># loop with range() function
\>>>for x inrange(5):
\>>>print(x)01234
\>>>for x inrange(2,5):
\>>>print(x)234
\>>>for x inrange(2,10,3):
\>>>print(x)258
我们还可以使用else关键字在循环结束时执行一些语句。
在循环结束时提供else语句以及循环结束时需要执行的语句。
\>>>for x inrange(5):
\>>>print(x)
\>>>else:
\>>>print("finished")01234
finished
\>>> msg ='Hello World'
\>>>print(msg)
Hello World
\>>>print(msg\[1\])
e
\>>>print(msg\[\-1\])
d
\>>>print(msg\[:1\])
H
\>>>print(msg\[1:\])
ello World
\>>>print(msg\[:\-1\])
Hello Worl
\>>>print(msg\[::\-1\])
dlroW olleH
\>>>print(msg\[1:5\])
ello
\>>>print(msg.upper())
HELLO WORLD
\>>>print(msg.lower())
hello world
\>>>print(msg.startswith('Hello'))True
\>>>print(msg.endswith('World'))True
\>>>print(', '.join(\['Hello','World','2022'\]))
Hello, World,2022
\>>>print(' '.join(\['Hello','World','2022'\]))
Hello World 2022
\>>>print("Hello World 2022".split())
\['Hello','World','2022'\]
\>>>print("Hello World 2022".rjust(25,'-'))
\---------Hello World 2022
\>>>print("Hello World 2022".ljust(25,'\*'))
Hello World 2022\*\*\*\*\*\*\*\*\*
\>>>print("Hello World 2022".center(25,'#'))#####Hello World 2022####
\>>> name ="Codemaker"
\>>>print("Hello %s"% name)
Hello Codemaker
\>>>print("Hello {}".format(name))
Hello Codemaker
\>>>print("Hello {0}{1}".format(name,"2022"))
Hello Codemaker2022
13.正则表达式
导入regex模块,import re。
re.compile()使用该函数创建一个Regex对象。
将搜索字符串传递给search()方法。
调用group()方法返回匹配的文本。
\>>>import re
\>>> phone\_num\_regex = re.compile(r'\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d')
\>>> mob = phone\_num\_regex.search('My number is 996-190-7453.')
\>>>print('Phone number found: {}'.format(mob.group()))
Phone number found:996\-190\-7453
\>>> phone\_num\_regex = re.compile(r'^\\d+$')
\>>>is\_valid = phone\_num\_regex.search('+919961907453.')isNone
\>>>print(is\_valid)True
\>>> at\_regex = re.compile(r'.at')
\>>> strs = at\_regex.findall('The cat in the hat sat on the mat.')
\>>>print(strs)
\['cat','hat','sat','mat'\]