设为首页 加入收藏

TOP

Python基础语法和Ubuntu常用命令(人工智能实践笔记)
2019-04-25 02:50:59 】 浏览:86
Tags:Python 基础 语法 Ubuntu 常用 命令 人工智能 实践 笔记

linux常用终端命令

:~$  pwd    当前路径
:~$  ls     有什么文件
:~$  mkdir python 当前目录建立名为python目录
:~$  ls
:~$  cd python  进入python目录
:~$  python  进入python解释器 

注意:使用pwd命令打印当前在哪个目录,打印的是绝对路径。
绝对路径:是以根目录 (“/”) 为起点的完整目录,为你所要到的目录为终点。
相对路径:是以根目录 (“.”) 为起点的完整目录,为你所要到的目录为终点。
在这里插入图片描述

常用的基础语法点

运算符:+、-、*、/、% (巴拉巴拉省略)

列表:

 c=[1,2,3,4,5,6,7]       
d=[“张三”,"李四",“王五”]

索引: d[0] 表示张三
用列表名[起:止]表示切片,从列表中切出相应的元素,前闭后开

 c[0:2]切出[1,2]
 c[ : ]切出[1,2,3,4,5,6,7]

用列表名[起:止:步长] 带步长的切片,步长有方向
切出[5,4,3,2]用c[4:0:-1]
切出[5,4,3,2,1]用c[4: :-1]
切出[6,4,2]用c[-2: :-2] 从倒数第二个一直到开头,步长-2

修改:列表名[索引号]=新值
删除:del 列表名[索引号]
插入:列表名.insert(插入位置索引号,新元素)

练习1:

cd python
c=[1,2,3,4,5,6,7]
c[4]
c[0: :2]
c[-1:0:-3]
c[3]=8
c
c.insert(3,38)
c
del c[3]
c

在这里插入图片描述

元组

元组( )一旦定义不能改变
f=(1,2,3)

字典

{键:值,键:值,键:值} n个键值对儿

dic={1:"123","name":"zhangsan","height":180}

用字典名[键] 索引字典中的值
dic[“name”]表示“zhangsan”
修改、删除、插入同上
练习2:

cd python
python
dic={1:"123","name":"zhangsan","height":180}
dic["name"]
dic["height"]=178
dic
del dic["name"]
dic
dic[“age"]=18
dic

**
在这里插入图片描述

vim编辑器

在这里插入图片描述

条件语句

if 条件:
    操作
elif 条件:
    操作
else
    操作

练习3:

#coding:utf-8  注释兼容中文
age=input("输入你的年龄\n")
if age>18:
    print "大于十八岁"
else:
    print "小于十八岁"

在这里插入图片描述

循环语句

第一种:
for 变量 in range(开始值,结束值):
      操作

练习4:

cd python 
python
for i in range(0,5):
...     print "hello world"
    
for i in range(0,5):
...     print "hello world %s" %i

在这里插入图片描述

第二种:
for 变量 in 列表名

练习五:

cd python
python
h=["a","b","c","d"]
for i in h:
    print i
    for j in h:
            print j

在这里插入图片描述

while条件

while 条件:
    操作

练习六:

cd python
python
x=1
y=2
while x<5 and y<5:
    x=x+1
    y=y+1
    print  x,y

beak 终止循环
在这里插入图片描述

turtle模块

import turtle     #导入turtle模块
t=turtle.Pen()  #用turtlr模块中的Pen类,实例化出一个叫做t的对象
t.forword(像素点)   #让t向前走多少个像素点
t.backward(像素点) #向后走多少像素点
t.left(角度)   #让t向左转多少度
t.right(角度)  #让t向右转多少度
t.reset()  #让t复位

练习七:

cd python
python
import turtle
t=turtle.Pen()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)

在这里插入图片描述
for和while循环实现:

cd python
python
import turtle
t=turtle.Pen()
for i in range(0,4):
    t.forward(100)
    t.letf(90)
t.reset()
i=0
while True:
    t.forward(100)
    t.left(90)
    i=i+1
    if i==4:
        break

在这里插入图片描述

函数、模块、包

函数:

函数定义:def 函数名 (参数表):
           函数体
 使用函数:函数名(参数表)  如:input("please go out")
函数返回值:return
def add(a,b)
    return a+b
c=add(5,6)
#c被复制为add的返回值11
内建函数:python解释器自带函数
abs(-10)  返回10

**模块:**函数的集合,先导入,再使用,用模块,函数名调用。

import time
time.asctime()

:包含多个模块

from PIL import image

类、对象、面向对象

类:是函数的集合,可实例化出对象的模具。
实例化:对象=类() t=turtle.Pen()
对象:是实例化出的实体,对象实实在在存在,完成具体工作
面向对象:程序员反复修改优化类,类实例化出对象,对象调用类里面的函数执行具体操作。

类的定义:

class 类名(父类名)

对象调用类里的函数,用 对象.函数名
对象调用类里的变量,用 对象.变量名
类内定义函数时,如调用自身或父亲的函数与变量,必须用**self.**引导,应该写为 self.函数名self.变量名

文件写操作

开:文件变量=open (“文件路径文件名”,“wb”)
存:pickle.dump(待写入的变量,文件变量)
关:文件变量.close()

练习8

import pickle
game_data={"position":"N2 E3","pocket":["key","knife"],"money":160}
save_file=open("save.dat","wb")
pickle.dump(game_data,save_file)
save_file.close()
exit()

ls
vim save.dat    此时的代码是序列,不具有可读性
python
import pickle
load_file=open("save.dat","rb")
load_game_data=pickle.load(load_file)
load_file.close()
load_game_data  会看到内容

在这里插入图片描述
在这里插入图片描述

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇python爬虫知识体系 python &nbs.. 下一篇第二章 Python编程基础知识  ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目