记录一下Python学习

#20210325

think@MyMacBook ~ % python

WARNING: Python 2.7 is not recommended.

This version is included in macOS for compatibility with legacy software.

Future versions of macOS will not include Python 2.7.

Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Dec 21 2020, 23:00:36)

[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin

Type "help", "copyright", "credits" or "license" for more m.

 

#导入该模块

>>> import platform

 

#输出uname函数

>>> print platform.uname()

('Darwin', 'MyMacBook', '20.3.0', 'Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64', 'x86_64', 'i386’)

 

#查看发行版本

>>> print platform.release()

20.3.0

 

#查看该模块下的所有函数

>>> dir(platform)

['DEV_NULL', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__copyright__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_abspath', '_architecture_split', '_bcd2str', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_get_real_winver', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_lsb_release_version', '_mac_ver_gestalt', '_mac_ver_lookup', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_popen', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'string', 'sys', 'system', 'system_alias', 'uname', 'version', 'win32_ver']

 

#示例1

>>> students=['a','b','c','d','e’]

 

>>> random.choice(students)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'random' is not defined  #报错 random未定义

 

#导入该模块random

>>> import random

 

>>> random.choice(students)  写错了

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'students' is not defined

 

#哈哈 随机输出哦

>>> random.choice(sutdents)

'c'

>>> random.choice(sutdents)

'e'

>>> random.choice(sutdents)

'c'

>>> random.choice(sutdents)

‘d'

 

#20210326

think@MyMacBook scripts % cat 20210326.py

#!/usr/bin/python

#

name="Tiger"

def printName(a):

    print a

num=3

print num

printName(name)

 

#20210329

>>> my_name="Tiger"

>>> print my_name

Tiger

>>> print my_name[0]

T

>>> print my_name[1]

i

>>> print my_name[2]

g

>>> print my_name[3]

e

>>> print my_name[4]

r

哈哈,牛逼的python

 

“=”用于将变量名与内存中存在的某对象绑定,如 该对象已存在直接绑定,如不存在重新创建

 

>>> l1=["This","is","a","tiger"]

>>> l1

['This', 'is', 'a', 'tiger']

>>> l1[0]

'This'

>>> l1[0][0]

'T'

>>> l1[0][1]

'h'

>>> l1[0][2]

'i'

>>> l1[0][3]

’s’

 

#字符串切片演练

s[I] 索引运算符

s[i:j] 切片运算符

s[i:j:stride] 扩展切片运算符

 

 

 

>>> name="Tiger"

>>> name[0]

'T'

>>> name[0:1]

'T'

>>> name[0:2]

'Ti'

>>> name[0:4]

'Tige'

>>> name[0:5]

'Tiger'

 

 

>>> name[5:]

''

>>> name[1:]

‘iger'

#从第1个到第3个字符,步长为2

>>> name[0:3:2]

‘Tg'

 

#类型、变量

>>> name="Tiger"

>>> test="Tiger"

>>> type(name)

<type 'str'>

>>> type(test)

<type 'str'>

>>> type(name) is type(test)

True

 

 

>>> name="Tiger"

>>> name.upper()

‘TIGER'

Python中一切皆对象,对象皆可方法

 

#重点来了 key value

>>> d1={'a':01,'b':02}

>>> d1['a']

1

>>> d1['b']

2

>>> d1={1:011,2:022}

>>> d1[1]

9

>>> d1[2]

18

>>> 011 is 9

True

>>> 022 is 18

True

哈哈 真逗

 

#写了一个函数

think@MyMacBook scripts % cat 20210326.py

 

#!/usr/bin/python

#

name="Tiger"

def printName(a):

    print name.upper()

num=3

print num

printName(name)

 

#20210330

#字符串的+操作  

>>> l1=[1,2,3]

>>> l2=['a','b','c']

#l1的内存地址

>>> id(l1)

4466667600

#l2的内存地址

>>> id(l2)

4466749952

#拼接l1 和 l2

>>> l1+l2

[1, 2, 3, 'a', 'b', 'c']

>>> id(l1+l2)

4466825480

即 两个字符串拼接

 

#字符串元素的*操作

>>> l1*3

[1, 2, 3, 1, 2, 3, 1, 2, 3]

即 l1的字符串重复3次

 

#字符串元素的 in  or  not in 操作

>>> 2 in l1

True

>>> 222 in l1

False

 

 

>>> l3=[('a',1),('b',2),('c',3)]

 

>>> print l3

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

>>> d03=dict(l3)

>>> print d03

{'a': 1, 'c': 3, 'b': 2}

 

#从d03中取出key为a的value值

>>> print d03['a']

1

 

 

>>> print l3

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

>>> d03=dict(l3)

>>> print d03

{'a': 1, 'c': 3, 'b': 2}

#取出第一个value

>>> print d03[1]

1

 

>>> l3.append(4)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute ‘append'

 

>>> l3

(1, 2, 3)

>>> l3.insert(5)

Traceback (most recent call last):

  File "<stdin>", line 1

    help insert

              ^

SyntaxError: invalid syntax

 

 

>>> l3.count()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: count() takes exactly one argument (0 given)

 

#计算l3中value为1的计数

>>> l3.count(1)

1

>>> l3.extend(5)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'extend'

>>> l3.extend(15)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'tuple' object has no attribute 'extend'

 

>>> l3=[1,2,3]

>>> l3

[1, 2, 3]

>>> l3.count(1)

1

>>> l3.count(2)

1

>>> l3.count(3)

1

>>> l3.index(3)

2

>>> l3.index(2)

1

>>> l3.index(1)

0

>>> l3.extend(4)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not iterable

 

>>> l3.extend('a')

>>> l3.extend('b')

>>> l3

[1, 2, 3, 'a', 'b']

 

>>> l3.insert(4)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: insert() takes exactly 2 arguments (1 given)

 

#在index位置为1处插入5

>>> l3.insert(1,5)

>>> l3

[1, 5, 2, 3, 'a', 'b’]

 

#在index位置为0处插入5

>>> l3.insert(0,5)

>>> l3

[5, 1, 5, 2, 3, 'a', 'b’]

 

#字符串的更新操作

>>> d1={'x':1,'y':2}

>>> d2={'m':21,'n':76,'y':44}

 

>>> d1

{'y': 2, 'x': 1}

>>> d1.update(d2)

>>> d1

{'y': 44, 'x': 1, 'm': 21, 'n': 76}

 

#从数组承接数据

>>> d1.iteritems()

<dictionary-itemiterator object at 0x10a3d0ba8>

>>> i1=d1.iteritems()

>>> i1.next()

('y', 44)

>>> i1.next()

('x', 1)

>>> i1.next()

('m', 21)

>>> i1.next()

('n', 76)

 

>>> i2=d1.iterkeys()

>>> i2.next()

'y'

>>> i2.next()

'x'

>>> i2.next()

'm'

>>> i2.next()

’n'

 

>>> i2=d1.iteritems()

>>> i2.next()

('y', 44)

>>> i2.next()

('x', 1)

>>> i2.next()

('m', 21)

>>> i2.next()

('n', 76)

 

>>> i3=d1.itervalues()

>>> i3.next()

44

>>> i3.next()

1

>>> i3.next()

21

>>> i3.next()

76

 

#20210331

#创建 列表l3,列表共3个元组每个元组都是 k:v 的元素对儿

>>> l3=[('a',1),('b',2),('c',3)]

>>> print l3

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

#把列表l3转变为字典

>>> d1=dict(l3)

>>> print d1

{'a': 1, 'c': 3, 'b': 2}

哈哈,kv结构很明显了吧

>>> print d1['a']

1

>>> print d1['b']

2

>>> print d1['c']

3

 

#对象的三种表现形式

字符串  

 单引号 ‘A'

 双引号 “A”

 三个单引号 ‘’’A‘’’ 

 三个双引号 “””A“””

列表 [] 

元组 ()

 

>>> l1=[1,2,6,4,9,3]

>>> print l1

[1, 2, 6, 4, 9, 3]

>>> l1.reverse()

>>> print l1

[3, 9, 4, 6, 2, 1]

>>> l1.reverse(l1.sort)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: reverse() takes no arguments (1 given)

>>> l1.sort()

>>> print l1

[1, 2, 3, 4, 6, 9]

>>> l1.reverse;

<built-in method reverse of list object at 0x107131878>

>>> l1.reverse();

>>> l1.sort()

>>> print l1

[1, 2, 3, 4, 6, 9]

创建时间:2021-12-22 16:26
浏览量:0