【Hello world!】《python编程从入门到实践》

一.linux系统中的python

1.检查python的版本

    进入python窗口    Ctrl+Alt+T(ubuntu)

#查看python版本
$ python
python 2.7.6(......)
>>>

    退出python窗口   Ctrl+D或者exit()

2.检查是否装了Python3

#查看python版本
$ python3
python 3.5.0(......)
>>>

 

二.变量和简单数据类型

>>>#name.py
>>>name='ada love'
>>>print(name.title())
Ada Love

【方法 】:是python对数据执行的操作

【(.)】:让python对name变量执行方法title( )的制定操作。

     常用方法:【   .title( ) 】:

                      【 .lower( )】:无法依靠用户提供正确的大小写时候,先转化为小写存储或比较。按需转换。

                      【   .str( )   】:避免数字和字符串拼接时候的类型错误!

>>>age=23
>>>message="Happy" + str(age) + "rd Birthday"
>>>print(message)
Happy 23rd Birthday

注意:在Python3中-print是一个函数了,( )括号必不可少。

           python2中的整数相除不精确

>>>python2.7
>>>3/2
1
>>>3.0/2
1.5
>>>3/2.0
1.5
>>>3.0/2.0
1.5

 

三.列表简介

列表:一系列特定顺序排列的元素组成。【元素之间可以没有关系】

列表是有序集合,所以可以通过元素的位置或者索引访问列表元素。

>>>#bicycles.py
>>>bicycles=['trek','cannondale','redline','specialized']
>>>print(bicycles)
['trek','cannondale','redline','specialized']

大多数列表都是动态的

1.修改列表元素       :  利用访问索引下标修改。

2.在列表中添加元素:

3.从列表中删除元素:

>>>motorcycles=['honda','yamaha','suzuki']
>>>#修改元素
>>>motorcycles[0]='ducati'         
>>>print(motorcycles)
['ducati','yamaha','suzuki']
>>>#列表末端添加元素,不影响其他元素
>>>motorcycles.append('honda')
>>>print(motorcycles)
['ducati','yamaha','suzuki','honda']
>>>#列表中插入新元素,影响其后面位置的元素
>>>motorcycles.insert(0,'abc')
>>>print(motorcycles)
['abc','ducati','yamaha','suzuki','honda']
>>>#1.使用del语句【不是方法】删除指定位置的元素
>>>del motorcycles[0]
>>>print(motorcycles)
['ducati','yamaha','suzuki','honda']
>>>#2.pop()方法默认删除弹出末尾元素,或者其他位置元素,并可以使用这个弹出的值
>>>popped_moto=motorcycles.pop()
>>>print(popped_moto)
>>>print(motorcycles)
honda
['abc','ducati','yamaha','suzuki']
>>>first_owned=motorcycles.pop(0)
>>>print(first_owned)
abc
>>>#根据值删除元素,remove()方法,可以使用被删除的值
>>>#如果要删除的值在列表中出现不止一次,只删除第一个。如果想删除多个--用循环判断!
>>>too_expensive='ducati'
>>>motorcycles.remove(too_expensive)
>>>print(motorcycles)
['yamaha','suzuki']

组织列表【排序】

【   .sort()   】      对列表进行永久性排序

.sorted() 】      对列表进行临时排序

.reverse()】       对列表进行永久性的颠倒排序

【     .len()  】     

>>>cars=['bmw','audi','toyota','subaru']
>>>cars.sort()           #按照字母相反顺序排列的话需要传递参数“reverse=True”
>>>print(cars)
['audi','bmw','subaru','toyota']
>>>cars.sort(reverse=True)
>>>print(cars)
['toyota','subaru','bmw','audi']
>>>print(sorted(cars))   #也可以向函数sorted()传递参数reverse=True
['audi','bmw','subaru','toyota']

 

四.操作列表

遍历整个列表

例子:for cat in cats:

          for item in list_of_items:

使用单数和复数名称,可帮助你判断代码处理的单个列表元素还是整个列表。

创建数值列表

>>>number=list(range(1,6))
>>>print(number)
[1,2,3,4,5]
>>>even_numbers=list(range(2,11,2))
>>>print(even_numbers)
[2,4,6,8,10]
>>>squares=[]
>>>for value in range(1,11):
...    square=value**2
...    squares.append(square)    #squares.append(value**2)
>>>print(squares)
[1,4,9,16,25,36,49,64,81,100]


>>>digits=[1,2,3,4,5,6,7,8,9,0]
>>>min(digits)
0
>>>max(digits)
9
>>>sum(digits)
45

列表解析

列表解析:将for循环和创建新元素的代码合并成一行

>>>squares[values**2 for value in range(1,11)]   #注意这里面有冒号
>>>print(squares)
[1,4,9,16,25,36,49,64,81,100]

使用列表的一部分【切片】

遍历切片

>>>for player in plays[:3]:
...    print(player.title())
...

复制列表

          ||__创建一个包含整个列表的切片(同时省略起始索引终止索引

>>>my_foods=['pizza','falafel','carrot cake']
>>>friend_foods=my_foods[:]    #省略索引
#错误演示
#下面只是引用赋值,两个引用指向同一组数据,并非创建新的副本。只有切片能够达到复制列表生成新的副本
>>>my_foods=['pizza','falafel','carrot cake']
>>>friend_foods=my_foods
>>>my_foods.append('cannoli')
>>>friend_foods.append('ice cream')
>>>print(my_foods)
>>>print(friends_foods)
['pizza','falafel','carrot cake','cannoli','ice cream']
['pizza','falafel','carrot cake','cannoli','ice cream']

元组

列表,非常适合用于存储在程序运行期间可能变化的数据集。

元组,不可修改的元素的列表。

元组看起来犹如列表,可以使用数字索引来访问元组中元素,但是不可以修改元组其中的元素,并且不能给元组其中的元素赋值。

【修改元组变量】

          ||__可以重新定义整个元组。

五.if语句

#cars.py
>>>cars=['audi','bmw','subaru','toyota']
>>>for car in cars:
...    if car == 'bmw':
...        print(car.upper())
...    else:
...        print(car.title())

and和or关键词检查多个条件

>>>age_0 >= 21 and age_1 >= 21
True
>>>age_0 >= 21 or age_1 >= 21
True

in  检查特定值是否包含在列表中

not in  检查特定值未包含在列表中

>>>requested_toppings = ['mushroom','onions','pineapple']
>>>'mushroom' in requested_toppings
True
>>> user = 'marie'
>>>if user not in requested_toppings:
...    print(requested_toppings)

布尔表达式【开关】

布尔表达式是条件测试的别名。

跟踪程序状态或者程序中重要条件的方面,布尔表达式提供了一种高效的方式。

>>>game_active=True
>>>can_edit=True

 

转载于:https://my.oschina.net/woailuo/blog/808892

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值