Python基础

news/2024/7/19 9:25:15 标签: python, 爬虫, shell

前言

Python,是龟叔在1989年为了打发无聊的圣诞节而编写的一门编程语言,特点是优雅、明确、简单,现今拥有丰富的标准库和第三方库。
Python适合开发Web网站和各种网络服务,系统工具和脚本,作为“胶水”语言把其他语言开发的模块包装起来使用,科学计算等等。

小编学习Python的理由有三个:
为了爬取需要的各种数据,不妨学习一下Python。
为了分析数据和挖掘数据,不妨学习一下Python。
为了做一些好玩有趣的事,不妨学习一下Python。

准备工作

1、在Python官网下载安装喜欢的版本,小编使用的,是当前最新版本3.6.0。

2、打开IDLE,这是Python的集成开发环境,尽管简单,但极其有用。IDLE包括一个能够利用颜色突出显示语法的编辑器、一个调试工具、Python Shell,以及一个完整的Python3在线文档集。

hello world

1、在IDLE中,输入print('hello world'),回车,则打印出hello world。
PS:语句末尾加不加分号;都可以,小编决定不加分号,更简单。

2、使用sublime新建文件hello.py,内容如下:

print('hello world')

在Windows下,shift+右键,在此处打开命令窗口,执行python hello.py,回车,则打印出hello world。

3、使用sublime新建文件hello.py,内容如下:

#!/usr/bin/env python
print('hello world')

在Linux或Mac环境下,可以直接运行脚本。首先添加执行权限chmod a+x hello.py,然后执行./hello.py。当然,也可以和Windows一样,使用python hello.py来执行脚本。

引入模块

1、新建name.py,内容如下:

name='voidking'

2、执行python name.py
3、进入python shell模式,执行import nameprint(name.name),则打印出voidking。

基础语法

常用函数(print)、数据类型、表达式、变量、条件和循环、函数。和其他语言类似,下面选择一部分展开。

list链表数组

1、定义数组
myList = ['Hello', 100, True]
2、输出数组
print(myList)
3、输出数组元素
print(myList[0])print(myList[-1])
4、追加元素到末尾
myList.append('voidking')
5、追加元素到头部
myList.insert(0,'voidking')
6、删除元素
myList.pop()myList.pop(0)
7、元素赋值
myList[0]='hello666'

tuple固定数组

1、定义数组
myTuple = ('Hello', 100, True)
错误定义:myTuple1=(1),正确定义:myTuple=(1,)
2、输出数组
print(myTuple)
3、输出数组元素
print(myTuple[0])
4、tuple和list结合
t = ('a', 'b', ['A', 'B'])t[2][0]='X'

if语句

if

score = 75
if score>=60:
    print 'passed'

两次回车,即可执行代码。

if-else

if score>=60:
    print('passed')
else:
    print('failed')

if-elif-else

if score>=90:
    print('excellent')
elif score>=80:
    print('good')
elif score>=60:
    print('passed')
else:
    print('failed')

循环

for循环

L = [75, 92, 59, 68]
sum = 0.0
for score in L:
       sum += score
print(sum / 4)

while循环

sum = 0
x = 1
while x<100:
    sum += x
    x = x + 1
print(sum)

break

sum = 0
x = 1
while True:
    sum = sum + x
    x = x + 1
    if x > 100:
        break
print(sum)

continue

L = [75, 98, 59, 81, 66, 43, 69, 85]
sum = 0.0
n = 0
for x in L:
    if x < 60:
        continue
    sum = sum + x
    n = n + 1
print(sum/n)

多重循环

for x in ['A', 'B', 'C']:
    for y in ['1', '2', '3']:
        print(x + y)

dict

dict的作用是建立一组 key和一组value的映射关系。

d = {
    'Adam': 95,
    'Lisa': 85,
    'Bart': 59,
    'Paul': 75
}
print(d)
print(d['Adam'])
print(d.get('Lisa'))
d['voidking']=100
print(d)
for key in d:
    print(key+':',d.get(key))

set

set持有一系列元素,这一点和list很像,但是set的元素没有重复,而且是无序的,这点和dict的key很像。

s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print(s)
s = set(['Adam', 'Lisa', 'Bart', 'Paul', 'Paul'])
print(s)
len(s)
print('Adam' in s)
print('adam' in s)
for name in s:
    print(name)
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
    print(x[0]+':',x[1])
s.add(100)
print(s)
s.remove(('Adam',95))
print(s)

函数

自带函数

del sum
L = [x*x for x in range(1,101)]
print sum(L)

自定义函数

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

引入函数库

import math

def quadratic_equation(a, b, c):
    x = b * b - 4 * a * c
    if x < 0:
        return none
    elif x == 0:
        return -b / (2 *a)
    else:
        return ((math.sqrt(x) - b ) / (2 * a)) , ((-math.sqrt(x) - b ) / (2 * a))
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))

可变参数

def average(*args):
    if args:
        return sum(args)*1.0/len(args)
    else:
        return 0.0

print(average())
print(average(1, 2))
print(average(1, 2, 2, 3, 4))

切片

list切片

L = ['Adam', 'Lisa', 'Bart', 'Paul']
L[0:3]
L[:3]
L[1:3]
L[:]
L[::2]

倒序切片

L[-2:]
L[-3:-1]
L[-4:-1:2]
L = range(1, 101)
L[-10:]
L[4::5][-10:]

PS:range是有序的list,默认以函数形式表示,执行range函数,即可以list形式表示。

字符串切片

def firstCharUpper(s):
    return s[0:1].upper() + s[1:]

print(firstCharUpper('hello'))

迭代

Python的for循环不仅可以用在list或tuple上,还可以作用在其他任何可迭代对象上。
迭代操作就是对于一个集合,无论该集合是有序还是无序,我们用for循环总是可以依次取出集合的每一个元素。
集合是指包含一组元素的数据结构,包括:

  • 有序集合:list,tuple,str和unicode;

  • 无序集合:set

  • 无序集合并且具有key-value对:dict

for i in range(1,101):
    if i%7 == 0:
        print(i)

索引迭代

对于有序集合,元素是有索引的,如果我们想在for循环中拿到索引,怎么办?方法是使用enumerate()函数。

L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
    print(index+1, '-', name)

myList = zip([100,20,30,40],L);
for index, name in myList:
    print(index, '-', name)

迭代dict的value

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
print(d.values())
for v in d.values():
    print(v)

PS:Python3.x中,dict的方法dict.keys(),dict.items(),dict.values()不会再返回列表,而是返回一个易读的“views”。这样一来,k = d.keys();k.sort()不再有用,可以使用k = sorted(d)来代替。
同时,dict.iterkeys(),dict.iteritems(),dict.itervalues()方法不再支持。

迭代dict的key和value

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
for key, value in d.items():
    print(key, ':', value)

列表生成

一般表达式

L = [x*(x+1) for x in range(1,100)]
print(L)

复杂表达式

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
    if score >=60:
        return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
    else:
        return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)

tds = [generate_tr(name,score) for name, score in d.items()]
print('<table border="1">')
print('<tr><th>Name</th><th>Score</th><tr>')
print('\n'.join(tds))
print('</table>')

条件表达式

L = [x * x for x in range(1, 11) if x % 2 == 0]
print(L)
def toUppers(L):
    return [x.upper() for x in L if isinstance(x,str)]

print(toUppers(['Hello', 'world', 101]))

多层表达式

L = [m + n for m in 'ABC' for n in '123']
print(L)
L = [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(1,10) if a==c]
print(L)

后记

至此,Python基础结束。接下来,爬虫飞起!

书签

Python官网
https://www.python.org/

Python入门
http://www.imooc.com/learn/177

如何学习Python爬虫[入门篇]?
https://zhuanlan.zhihu.com/p/...

你需要这些:Python3.x爬虫学习资料整理
https://zhuanlan.zhihu.com/p/...


http://www.niftyadmin.cn/n/1377867.html

相关文章

lvs作为负载均衡 用到什么模式

我们公司用的是DR模式&#xff0c;首先说LVS的三种模式分别是&#xff1a;DR、NAT、TUN&#xff0c;DR模式相比是这三种模式中性能最高的&#xff0c;同时在负载方面也是真实服务最多的&#xff0c;所以我们公司使用的是DR模式 1、什么是LVS&#xff1f; 首先简单介绍一下LVS …

vue-cli3.0中vue.config.js基本配置、引入插件、设置代理、生成日期文件夹的方式...

代码如下&#xff1a;const moment require(moment); const now moment().format("YYYYMMDDHHmm"); //生成带日期的文件夹 const UglifyJsPlugin require(uglifyjs-webpack-plugin); //去console插件 module.exports { baseUrl: ./, // 基本路径 outputDir: di…

P1473 校门外的树3

时间: 1000ms / 空间: 131072KiB / Java类名: Main描述 校门外有很多树&#xff0c;有苹果树&#xff0c;香蕉树&#xff0c;有会扔石头的&#xff0c;有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树&#xff0c;保证任一时刻不会出现两段相同种类的树&…

文本处理三剑客之grep

grep grep(支持基本正则表达式)&#xff0c;egrep(支持扩展的正则表达式)&#xff0c;fgrep(快速的grep&#xff0c;不支持正则表达式) grep是一个最初用于Unix操作系统的命令行工具。在给出文件列表或标准输入后&#xff0c;grep会对匹配一个或多个正则表达式的文本进行搜索&a…

linux每日命令(13):more命令

阅读目录(Content)一&#xff0e;命令格式&#xff1a;二&#xff0e;命令功能&#xff1a;三&#xff0e;命令参数&#xff1a;四&#xff0e;常用操作命令&#xff1a;五. 使用实例1. 从第3行起显示log1文件中的内容2.从文件中查找第一个出现"五"字符串的行&#x…

[英 = 中] 从源文件构建 nginx

从源文件构建 nginx 原文地址: http://nginx.org/en/docs/configure.html 我们使用 configure 命令来设置构建 nginx 需要的配置。它定义了系统的各个方面&#xff0c;包括允许 nginx 用于连接处理的方法。最后&#xff0c;它会生成一个名为 Makefile 文件。这个 configure 命令…

事件和信号

信号和槽 示例效果如下&#xff1a; 示例代码&#xff1a; 1 #!/usr/bin/python32 # -*- coding: utf-8 -*-3 4 """5 ZetCode PyQt5 tutorial 6 7 In this example, we connect a signal8 of a QSlider to a slot of a QLCDNumber. 9 10 Author: Jan Bodnar 11…

DR模式原理

客户端向目标vip发出请求&#xff0c;lvs接收后根据负载均衡算法选择一台节点&#xff0c;将此节点的ip所在网卡的mac地址作为目标mac地址&#xff0c;发送到局域网里&#xff1b;节点在局域网中收到这个帧&#xff0c;拆开后发现目标IP(VIP)与本地匹配&#xff0c;于是处理这个…