【python基础】——安装,变量,函数,列表,字典,字符串

【python基础】——安装,变量,函数,列表,字典,字符串

Data:20180518(更新安装,变量,函数),20180519(更新列表,字典,字符串),更新完毕

参考资料:

廖雪峰https://www.liaoxuefeng.com/

慕课python教程:http://www.imooc.com/learn/317

textclass.net :http://www.testclass.net/python

《Automate the Boring stuff with Python》

 

一.python基础

1.1编程环境

a.线上编程:慕课,廖雪峰网站,菜鸟教程

下载learning.py

windows系统win+R 输入CMD,  输入e: 跳转到e盘,cd Document/python   

E:\Document\python> python learning.py

即可在线编程

b.本地编程:pycharm

 

1.2.#注释

#单行注释

'''

我是多行注释

'''

1.3.转移序列,多行文本

常用转义:

  • \n: 换行
  • \t: tab,可以当成是缩进
  • \: 反斜杠
  • \’: 单引号,可以在单引号字符串中打印出单引号
  • \”: 双引号,可以在双引号字符串中打印出双引号

多行文本

my_str="""this is the first line

this is the second line

this is the last line"""

1.4.python数据类型和变量

1.4.1整数(int),浮点数(float)

1.4.2字符串

1.4.3空值(None)

 

1.5拓展-输入输出

1.5.1 print() 函数,单个字符直接输出,多个字符间用逗号隔开,输出空格。

print("hello world")

1.5.2 input()函数

1.5.3 len()函数

1.5.4 str(),int(),float()函数

1.5.5 round()函数

 

二.控制流

2.1布尔值,比较操作符

布尔值:true ,false

比较操作符:<,>,=,<=,>=,!=(仅用于整型和浮点型值)

2.3 布尔操作符(与或非)

布尔操作符:and,not, or

操作顺序:not>and>or

 

2.5控制流语句

2.5.1 if语句,else语句,elif 语句

 if语句=if关键字+条件+冒号(:)+下一行开始缩进的代码块(if字句)

age = 20
if age >= 18:
    print 'your age is', age
    print 'adult'
print 'END'
if age >= 18:
    print 'adult'
elif age >= 6:
    print 'teenager'
elif age >= 3:
    print 'kid'
else:
    print 'baby'

 

2.5.2 while语句,break语句,continue语句

while语句=关键字(while )+条件+冒号(:)+while字句

N = 10
x = 0
while x < N:
    print x
    x = x + 1

使用break语句,退出循环体(全部)

使用continue语句,退出此次循环,跳过后续代码,继续下一次循环

 

2.5.3 for 循环和range()函数

(1)for()

for i in range(5): i in range(5):

等同于

for (i=0;i<5;i++){
}

(2) range()函数

range(x , y )等同于  x<=i<y ,一个半开半闭区间

range(x, y, step) 起始值,终止值,步长=》每次迭代后循环变量增加的值,可正可负起

 

2.5.4导入模块 import()语句

input(),len(),print()是python的内建函数,可以导入外部函数库,相当于c的 #include

inport语句=import关键字+模块名称+可选多个模块用逗号隔开

import math,random,sys,os

 

2.5.5 终止函数

sys.exit()

提前结束程序

 

三.函数

3.1定义函数def 

def my_abs(x):
    if x >= 0:
        return x
    else:
        return -x

return 返回值

 

3.2 global 语句全局变量

此时,赋值无法对global赋值语句赋值

 

3.3 小程序:猜数字

import random
aiNum = random.randint(1, 20)

for i in range(5):
     print("please enter a number range 1 to 20 "+"you have "+str(5-i)+" times")
     myNum = int(input())
     if myNum == aiNum:
         print("you are right the number is "+ str(aiNum))
     if myNum >aiNum:
         print("too high")
     elif myNum <aiNum:
         print("too low")
     else:
         break

ps: pycharm 好用

 

四.列表

类似于c中的数组 

4.1列表数据类型

4.1.1列表的基础

span[][],

span[0]=>列表中第一个

span[-1]=>列表中最后一个下标

切片:span【1:3】 =》开始到结束,但不包括结束

 

4.1.2列表涉及的函数

len(L)  获取L的长度

del  L[2]   删除L[2]

 

4.2列表的赋值

也可以用于字符串的链接

 

4.3方法

4.3.1 index()函数查找值

L=['hello','world','hi']
L.insert(3,'no')

index()函数,传入一个值,如果该值存在列表中,则返回它的下标

L=['hello','world','hi']
L.index('hi')

 

4.3.2  append()和insert()函数添加值

 append()在尾部添加值

L=['hello','world','hi']
L.append('no')

insert(3,'no')  新值的下标+要插入的新值

 

4.3.3  remove()函数删除值

L.remove('hi')    删除L列表中的hi元素,只删除一次

ps:知道具体的值用remove(),知道值的位置用del

 

4.3.4 sort()函数值排序

L=['hello','world','hi']
L.sort()

默认从小到大,按照ASCII码

逆序:

L.sort(reverse=True)

无视大小写:

L.sort(key=str.lower)

 

4.4元组

①L=('he','hi','world')    元组不可更改

②list(),tuple()用于元组和列表类型转换

 

4.5引用

4.5.1 列表就像指针

chess和span指向同一个存储位置【1,2,3,4,5】

改变chess【1】实际上是改变存储位置中【2】的值。

简单来说,列表就是指针形式,指向存储器中具体位置的值。

 

4.5.2  copy()和 deepcopy()函数

有上面可知,为了不改变原有的元素,选用copy模块,选择copy 函数,给chess一个新的位置

如果要复制的列表内还包含列表,则选用deepcopy()函数

 

五.字典和结构化数据

5.1字典数据类型

字典是无序的

span={ 'color':'red', 'name':'Carly', 'age':'13'}
for v in span.values():
    print(v)

 

5.1.1 字典的方法—键key() 和 值values() 和 键值对items()

 

5.1.2 判断是否存在

span={ 'color':'red', 'name':'Carly', 'age':'13'}
print('13' in span.values())

5.1.3 get()方法

get()函数获取key的值value,默认参数表示如果没有key,返回的值

span={ 'color':'red', 'name':'Carly', 'age':'13'}
print(span.get('age',0))

5.1.4 setdefault()  方法

在不检查的前提下,当key不存在字典里设置的默认值

span={ 'color':'red', 'name':'Carly', 'age':'13'}
print(span.setdefault('sex','man'))
span1={ 'color':'red', 'name':'Carly', 'age':'13','sex':'women'}
print(span1.setdefault('sex','man'))

5.2  pprint ()和pformat()打印

import pprint模块

 

六.字符串操作

6.1处理字符串

①转义字符

②切片

span='Hello World'
print(span[0])
print(span[-1])
print(span[3:])
print(span[:6])
print(span[3:6])

 

 

 

6.2字符串方法

6.2.1 字符串方法  大写upper(),小写lower(), 

isupper()=>判断是否全部大写

islower()=>判断是都全部小写

span='Hello World'
print(span.upper())

6.2.2 字符串方法isX

用于判断字符串组成

6.2.3 字符串方法 startwith() 忽和endwith()

用于判断字符串的开始和结尾

6.2.4字符串方法  join()spilt()

①join() 链接列表中的字符串

设置连接方式 

L=['my','name','is','Alice']
print(' '.join(L))
print('.'.join(L))
print('NO'.join(L))

②spilt()划分字符串中成列表

spilt('m')以字符串中的m划分

 

6.2.5 rjust() ljust()center() 对齐文本

'hello'.rjust(10)

总共位置是10,‘hello’在右对齐的位置

 

6.2.6strip(),rstrip()lstrip()删除空白字符

用于删除字符串左边/右边/两边的空白字符

 

6.2.7  pyperclip 模板 拷贝或者粘贴字符串

import  pyperclip

pyperclip.copy() 从计算机的剪切板获取文本

pyperclip.paste() 向计算机的剪贴板发送文本

 

 

七.集合

set() 过滤出重复的元素,无序排列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值