Python学习笔记1:Python基础

一 变量和类型
1 变量不需要声明


2 type():可查询数据类型


3 数据类型:None,int,float,bool,string


4 sequence:tuple-定值表,元素不可变,list-表,元素可变
s1 = (1, 2.2, "3", True) #s1 is a tuple
s2 = [1, 2.2, "3", True] #s2 is a list
s3 = [1,[2.2, "3", True]] #sequence is used as elem
元素引用
4.1 下标引用:s[(int)index],下标index从0开始:
s1[0] = 11
s3[1][0] = 22.22


4.2 范围引用:s[下限:上限:步长]
省略下限:从开始出遍历
省略上限:到最后
写明上限,则不包含上限本身


4.3 尾部引用:s[(int)-index],倒数第index个元素

5 词典

词典类似json的键值对

# 词典
dic = {"A":1,"B":2}
print type(dic)

dic["A"] = 10
dic["B"] = 11

for key, in dic:
    print dic[key]

# 元组(内容不可变)
ary = ("A",11)
print type(ary)

print ary[::]

# 表
list = ["A","B"]
print type(list)

list[0]="A"
list[1]=11

print list[::]


6 字符串
字符串是元组,可以执行元组的相关操作


7 运算
数学运算:+,-,*,/,%,**(乘方)
逻辑运算:==, !=, <, <=, >, >=, and, or, not, in(判断某个元素是不是序列的一个元素)


8 缩进
C语言:
if(i > 0)
{
    x = 1;
    y = 2;
}
Python:
if i > 0:
    x = 1
    y = 2


三 控制流程
1 选择结构 
if-elif-else
if condition
    do
elif condition
    do
else
    do


2 循环结构(支持continue和break)
2.1 for-in 遍历序列
for 元素 in 序列


2.2 while
while condition:
    do


2.3 range()

range可以传入三个参数,分别是起始值,终止值(不包含这个值),步长,类似C语言的for循环

string = 'python_loop'
for i in range(0,len(string),2):
    print string[i]

输出:

p
t
o
_
o
p


2.4  enumerate

返回包含下表和值两个元素的元组,然后赋值给给定的变量

string = 'python_loop'
for (i, c) in enumerate(string):
    print i,c

输出:

0 p
1 y
2 t
3 h
4 o
5 n
6 _
7 l
8 o
9 o
10 p


2.5 zip

每次循环,从等长的列表中,依次取出一个元素,组成一个元组

la = [1,2,3]
lb = [9,8,7]
lc = ['a','b','c']
for (a,b,c) in zip(la, lb, lc):
    print(a,b,c)
输出:

(1, 9, 'a')
(2, 8, 'b')
(3, 7, 'c')

可以用来聚合多个列表

la = [1,2,3]
lb = [9,8,7]
lc = ['a','b','c']
lzip = zip(la, lb, lc)
print(lzip)

ua,ub,uc = zip(*lzip)
print(ua, ub, uc)

输出:

[(1, 9, 'a'), (2, 8, 'b'), (3, 7, 'c')]
((1, 2, 3), (9, 8, 7), ('a', 'b', 'c'))


四 函数
格式:
def functionName(args)
    do
    return args(可返回多个参数)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值