---
title: "Python第一课-初识Python"
author: "yuanta11"
date: "2012-09-08T18:42:48Z"
tags: []
categories: ["编程语言"]
slug: "python-python"
draft: false
lastmod: "2012-09-09T14:05:31Z"
id: 1674063
url: "https://pythonmap.iteye.com/blog/1674063"
---

此笔记为原创，参考教材为中国电力出版社的《Head First Python》
全书用例为Python3.

一、Python安装(Head First Python采用Python3)：
    环境win7，Python版本3.2.3
    1、官网www.python.org下载Python3最新版本
    2、安装过程不表
    3、安装完成首在命令行中通过查看版本确定安装成功
        window:D:\python32\python -V　　linux:python3 -V

二、IDLE:
    1、IDLE是Python拿一送一的Python IDE(Python的集成开发环境)
    2、IDLE虽说简陋，可对于菜鸟级别的新手足够用了。自带shell，编辑器有代码提示(按下Tab)、代码着色。据说好些大牛平时也用。
    3、IDLE的shell中有代码回退（Alt-p)、代码前进(Alt-n)功能。
 

第一个习题：列表的数据处理

```python

cast = ['cleese', 'palin', 'jones', 'idle'] #创建列表并赋值给变量，变量无需声明（list-Python数据类型之一）
print(cast) #输出cast变量值（print()-Python BIF）
print(len(cast)) #输出cast变量值长度（len()-Python BIF）
print(cast[1]) #输出被赋值给cast变量的列表中的第2个元素
cast.append('Gilliam') #向cast中追加一个元素(append方法是列表自带方法)
cast.pop() #删除列表中最后一个元素，并return最后一个元素
cast.extend(['gilliam', 'chapman']) #向列表末尾追加另一个列表，另一个列表中元素作为目标列表中新元素
cast.remove('Chapman') #删除列表中指定元素
cast.insert(0, 'Chapman') #向列表中指定位置（此处为第一个元素前）插入一个元素

```


```python

'''列表的迭代'''

movies = ['movie1', 'movie2', 'movie3'] #创建列表并赋值给movies

'''for循环是处理列表内个元素的最常用方法
each_movie为目标标示符；movies为列表；print()代码块为列表元素处理代码'''
for each_movie in movies: 
    print(each_movie）

'''while循环是编写迭代代码的另一种备选方法
count 为一个计数标示符，用来表示列表状态信息'''
count = 0
while count < len(movies):
    print(movie[count])
    count += 1

```


```python

movie = ['the holy grail', 1975, 'director', 91,
['starring',
['actor1', 'actor2', 'actor3']]] #列表内元素可以是各种数据类型，可嵌套

'''使用if条件语句和for循环语句输出列表中嵌套的列表，本方法之判断嵌套的第一层列表'''
for each_item in movie:
    if isinstance(each_item, list): #isinstance()为判断条件，返回true or false；isinstance()为BIF，根据参数判断数据类型
        for each_item_deep1 in each_item: 
            print(each_item_deep1)
    else:
        print(each_item)

```


```python

'''创建一个递归函数解决多层嵌套列表的输出
pring_lol为函数名
the_list为参数'''

movie = ['the holy grail', 1975, 'director', 91,
['starring',
['actor1', 'actor2', 'actor3']]]

def print_lol(the_list):
    for each_item in the_list:
        if isinstance(each_item, list):
            print_lol(each_item)
        else:
            print(each_item)

pirint_lol(movie) #函数调用

```


零碎：

    1、Python内置函数成为：BIF(built-in functions)，print()函数便是其中之一