基础语法
赋值
'''
赋值运算
'''
a = 1
b = 1.1
bc = 4+2j
bb = False
bd = True
c = 'string'
d = e = f = 1
g, h, i = 1, 2, "runoob"
四则运算
'''
四则运算
'''
a + b
a - b
a * b
a / b
a // b
a % b
a ** b
逻辑
and
or
not
比较运算
'''
比较运算
'''
a == b
a != b
a > b
a < b
a >= b
a <= b
字符串
'''
字符串
'''
str = 'hello world'
str[0]
str[1]
str[0:]
str[1:]
str[0:-1]
列表
'''
列表 List
'''
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
list1 + list2
print(list1[0:2])
print(list1[0:])
Tuple 元组
'''
Tuple 元组
元组的元素不能修改,用法类似列表
'''
tuple = (1,2,3,4)
集合
'''
Set 集合
自动去重复
'''
students1 = {'小明1', '小红1', '小绿1'}
students2 = {'小明2', '小红2', '小绿2'}
print(students1 - students2)
print(students1 | students2)
print(students1 & students2)
print(students1 ^ students2)
字典
'''
字典
字典是无序的对象集合
'''
tinydict = {'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'}
print(tinydict)
print(tinydict.keys())
print(tinydict.values())
语法
条件控制
if 表达式1:
语句
if 表达式2:
语句
elif 表达式3:
语句
else:
语句
elif 表达式4:
语句
else:
语句
循环
while 判断条件:
语句
else:
语句
for <variable> in <sequence>:
<statements>
else:
<statements>
函数
def 函数名(参数列表):
函数体
类
class ClassName:
<statement-1>
.
.
.
<statement-N>
模块
import os
import shutil
import glob
import sys
import re
import urllib
import zlib
import timeit
import unittest
import json
import xml
import time
import datetime
import thread
import math
import smtplib
import socket
练手