Python学习之路——初识Python

一、第一个程序Hello World:

1、打印输出Hello World:

Python2打印方法:

>>> print "hello world"
hello world

 

Python3打印方法:

>>> print("hello world")

hello world

注:Python3与Pytho2的区别是加了小括号。

 

2、以文件形式执行代码:

[root@Centos-01 s1]# vim hello.py
打开一个文件hello.py文件内写入以下内容:

print("hello world")

保存退出。

当我们执行代码文件时会出现如下错误:

[root@Centos-01 s1]# ./hello.py
./hello.py: line 1: syntax error near unexpected token `"hello world"'
./hello.py: line 1: `print("hello world")'

出现此错误的原因是系统不知道用什么语言来执行文件内的代码。

解决方法就是用以下命令执行:

[root@Centos-01 s1]# python hello.py
hello world

这样就会正常执行了。但是这样执行会很不方便。我们可以在代码文件中指定用什么语言来执行代码。

将hello.py文件内容修改成如下:

第一种方法:

#!/usr/bin/python
print("hello world")

第二种方法:

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

注:第一种方法是指定执行代码语言绝对路径。第二种方法是查找指定的语言。推荐使用第二种方法,因为这样可以提高代码的可移植性。

此时我们再来执行代码:

[root@Centos-01 s1]# python hello.py
hello world
[root@Centos-01 s1]# ./hello.py
hello world

OK,两种方法都可以执行了。

 

二、变量声明:

info = “hello world”

变量命名规则:

1、变量名只能以数字、字母、下划线组成。

2、第一个字符只能为字母、下划线但不能为数字。

3、不要使用内置变量名。内置变量就是python语言自身内部定义的变量名例如:type、input、impot等。

 

三、用户交互:

1、用户输入代码:

Python3.X代码:

input_name = input("请输入你的名:")
print(input_name)

执行结果:

请输入你的名:Earl
Earl

Python2.X代码:

input_name = raw_input("请输入你的名字:")
print input_name

执行结果:

请输入你的名:Earl
Earl

2、在Python3.x与Python2.x中用户交互模块有区别对应关系如下:

Python3.x                              Python2.x

input                                       raw_input

eval(input())                             input()

四、If条件判断与while循环:

(一)、if语句:

1、语法:

if 条件:

    print(“要输出信息”)

elif 条件:

    print(“要输出信息”)

else:

    print(“要输出信息”)

2、例句:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

info = input("请输入您的性别:")
if info == "man":
    print("你是一个帅哥。")
elif info == "girl":
    print("你是一位淑女。")
else:
    print("对不起不知道你的性别。")

 

(二)、while循环:1语法:
while 条件:
print("要输出信息")

 

例句:#!/usr/bin/env python
# -*- coding: utf-8 -*-

while True:
print("info")
注:以上语句为死循环语句。

 

(三)、while与if结合使用案例:场景:定义一个数字,然后去猜测这个数字,不论输入数字大于还是小于定义数字均打印相应提示,猜对后退出并只有三次机会:代码如下:#!/usr/bin/env python
# -*- coding: utf-8 -*-
number = 0
lucky_number = 5
while number < 3:
input_number = int(input("请输入0到9之间的任意数字:"))
if input_number > lucky_number:
print("你输入的数字大于幸运数字.")
elif input_number == lucky_number:
print("你输入正确幸运数字.")
break
else:
print("你输入的数字小于幸运数字.")
number += 1
else:
print("对不起,你的机会用完了。")

 

五、数据类型:(一)、数字:1、int(整型)2、long(长整型)3、float(浮点型)(二)、布尔值:1、真或假2、1或0(三)、字符串:“hello world”

万恶的字符串拼接:

python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空间,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。
(四)、字符串格式化:
1、普通格式化:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = "Earl"
age = 27
info = ("我的名字叫%s, 我的年龄%d.") % (name, age)
print(info)
输出:我的名字叫Earl,我的年龄27.注:字符串是%s;整数是%d;浮点数%f2、使用三引号格式化:#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = "Earl"
age = 27
address = "北京"

info = """我的名字:%s
我的年龄:%d
我的住址:%s""" % (name, age, address)
print(info)
输出:
我的名字:Earl
我的年龄:27
我的住址:北京
3、字符串切片:

2、删除字符串空格:

 

info.strip() #去除两边的空格

info.lstrip() #去除左边的空格

info.rstrip() #去除右边的空格

 

 

 

六、列表(list):1、创建一个列表:list_info = ["name", "age", "address", 10000]2、列表取值:list_info[0] #0代表列表元素的索引值,值是以0开始的。所以结果为name。list_info[1] #当中括号内是1时取是agelist_info[0:2] #取出列表0到2的元素,但是不包含索引值为2,也就小于2的值。所以结果是name和age。list_info[-1] #取出最后个值,所以结果为10000.list_info[:-1] #取出索引从0开始至倒数第二个字符,不包括-1的值,所以输出结果为name, age, address。3、查看列表方法:>>>dir(list_info)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
注:带有__为私有方法,我们使用不到。

 

4、各种方法演示:append方法是在列表末尾追加元素:list_info = ["name", "age", "address", 10000]
list_info.append("city")
print(list_info)
输出结果:
['name', 'age', 'address', 10000, 'city']

 

count方法是统计某个元素在列表里的个数:list_info = ["name", "age", "address", 10000, "age"]
info = list_info.count("age")
print(info)
输出结果:2

 

insert方法是在列表内插入一个元素:list_info = ["name", "age", "address", 10000]
list_info.insert(2, "job")
print(list_info)
输出结果:['name', 'age', 'job', 'address', 10000]注:insert方法中的数字2是索引值,是将新插入的元素放在2这个索引值的位置,而原有的元素全部后移一位。

 

index方法是取出index的索引值:list_info = ["name", "age", "address", 10000]
info = list_info.index("age")
print(info)
输出结果:1

 

pop方法是删除列表内的元素:list_info = ["name", "age", "address", 10000]
list_info.pop()
print(list_info)
输出结果:['name', 'age', 'address']注:如果pope括号内没有索引值则是删除列表最后一位。如果有索引值则删除索引值的元素。

 

remove方法删除列表元素:list_info = ["name", "age", "address", 10000]
list_info.remove("age")
print(list_info)
输出结果:['name', 'address', 10000]

 

sort方法是排序,但是列表内只能是int类型元素:(此方法只在python3.x版本是这样)list_info = [1, 34, 0, 6, 10000]
list_info.sort()
print(list_info)
输出结果:[0, 1, 6, 34, 10000]

 
 
 

七、元组(tuple):1、创建元组:tuple_info = ("name", 34, "age", "address", 10000)

 

2、查看元组方法:

>>> dir(tuple_info)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

3、元组使用方法:

元组的操作与列表一样。但是元组只有count与index两种方法。

count方法是统计某个元素在元组里的个数:

tuple_info = ("name", 34, "age", "name", "address", 10000)
info = tuple_info.count("name")
print(info)
输出结果:2

 

index方法是取出index的索引值:

tuple_info = ("name", 34, "age", "address", 10000)
info = tuple_info.index("age")
print(info)
输出结果:2

 

八、列表(list),字符串(str),元组(tuple)说明:

1、相同点:
切片、索引、len() 、in

2、不同点:
str:重新开辟空间
list:修改后,不变

3、元组(tuple):
不能修改

 

九、运算符

1、算数运算:+ - * / % ** //
2、比较运算:==  !=  <>   >=  <= >  <
3、赋值运算:=  +=  -=  *=  /=  %=  **/  //=
4、逻辑运算:and  or  not
5、成员运算:in  not in
6、份运算:is  is not    
7、运算:& | ^ ~  >> <<
posted @ 2016-01-03 17:16  Earl.宋  阅读(454)  评论(0编辑  收藏  举报