Python_day1

 

Python

PHP

enter

input()

 

output

print()

echo、var_dump() 、print、print_r

quit

exit()

exit()、die()

linux runs directly

add comments before code

#!/usr/bin/env python3

 

Permission settings

$ chmod a+x filename.py

 

Format

indent

curly braces

case

sensitive

Not sensitive

indentation

Tab and 4-space indentation are not allowed to mix

 

translation area

r''

 

newline

print('''content''')

\n

Boolean operations

and、or、not

&& || !

division

Ordinary division /, floor division (round down) //

/

Integer and floating point numbers

Python's integers have no size limit

Python's floating-point numbers have no size limit, but if they exceed a certain range, they are directly represented as inf (infinite)

 

String to bytes

string.encode('ascii')

string.encode('utf-8')

 

bytes to string

string.decode('ascii')

string.decode('utf-8')

In bytes, bytes that cannot be displayed as ASCII characters are displayed with \x##. Error when transcoding directly

string.decode('utf-8',errors = 'ignore')

 

string length

len ()

bytes will count the number of bytes when using len()

strlen()

utf-8 transcoding tag

file header

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

header('content-type:text/html;charset=utf-8');

formatted output

print('%s,%s' % ('tony', 'joe'))

%% Express%

%d

integer

%.nf

Floating point number, n is the reserved number of digits

%s

string

%x

hex integer

 

 

Python

PHP

sorted set list

一种有序的集合,可以随时添加和删除其中的元素,元素类型不限制

可以list里面嵌套list

list = [1,2,3,4,5]

类似redis中的有序集合

集合长度计算

len()

lsize(key)

索引访问

list[0],索引范围为0—(len(list)-1)

索引为-1时表示最后一个元素,反过来也同样不允许越界

lindex(key,0)

尾部添加

list.append(value)

rpush(key,value)

尾部删除

list.pop()

rpop(key)

指定索引添加

list.insert(index,value)

 

指定索引删除

list.pop(index)

 

集合排序

list.sort()

执行后直接改变list

 

集合嵌套

嵌套集合使用len()时候,只计算第一层元素数量,但是可以先用索引找到子元素,然后继续使用子元素的索引获得元素

 

元组tuple

一种不能修改的有序列表

tuple = (1,2,3,4,5)

定义的时候必须确定元素,如果是空的,直接使用tuple = ()表示

特殊情况:tuple中包含list,尽管list中内容可变,但是tuple的资源指向不会变化

注意:单元素的时候如果想表示元组必须加个逗号!

tuple = (1,),否则按小括号计算,定义为普通变量

 


 

Python

PHP

条件判断

依靠缩进判断执行顺序

 

:代替{}

 

elif简化表达

a = 100

if a>100:

    print(a)

elif a==100:

    print(0)

else:

    print(-a)

a = 100;

if (a>100) {

          print(a);

}elseif (a=100) {

          print(0);

}else{

          print(-a)

}

input结合条件判断

s = input('birth: ')

birth = int(s)

if birth < 2000:

    print('00前')

else:

    print('00后')

input输入的是字符串,直接与数值比较的时候会报错!

 

for循环

a = ["aaa","bbb","ccc"]

for s in a:

    print(s)

 

b = [1,2,3,4,5]

sum = 0

for num in b:

    sum = sum + num

print(sum)

 

while循环

sum = 0

a = 1

while a<=100:

    sum = sum + a

    a = a + 1

print(sum)

 

break

结束循环

 

continue

结束本次循环,执行下个循环

 

range生成整数序列

list(range(num))

可以生成0—num-1的整数list

 

 

Python

PHP

字典dict

键值对方式存储

dict = {"a":1,"b":2,"c":3}

查找和插入的速度极快,不会随着key的增加而变慢;需要占用大量的内存,内存浪费多。

空间换时间

dictkey必须是不可变对象

类似json

dict获取

key不存在的时候会报错

print(dict["a"])

print(dict.get(key,defaultValue))

不设置默认值的时候取不到返回none

 

判断key是否存在

print(key in dict)

 

dict数据插入

key一样的时候,后插入替换前面

dict["d"] = 4

 

dict数据删除

dict.pop(key)

 

无序集合set

一组key的集合,不存储value,

key不重复,重复的时候会自动过滤

以list作为输入集合

list = [1,3,2]

set = set(list)

print(set)

 

集合新增

set.add(key)

重复时能运行,不生效,自动去重

 

集合删除

set.remove(key)

 

set交集和并集

交集set1 & set2

并集set1 | set2

 


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326667712&siteId=291194637