首页 > 其他 > 详细

第一课基础知识

时间:2015-12-02 18:15:12      阅读:307      评论:0      收藏:0      [点我收藏+]
python中的print有几种常用的用法:
1. print("first example")
2. print("second", "example")
3.print("%s"%("third example"))
4. print("%(forth)s"%{'forth':'forth example'})
5.fifth = "fifth example"
print("%(fifth)s"%vars())
 

创建简单的python列表 ,name = ("alca", "kalpana","arvin"),在数据的两边加引号,将其转化为字符串。

用逗号将列表项与下一项分隔开。在列表的两家加上开始和结束中括号。用赋值操作符号= 将列表赋一个标示符

name = ("alca", "kalpana","arvin"),alca就是0 kalpana就是2 arvin就是3,使用括号记法来访问列表数据

print(name[1]) 输出kalpana,print(len(name)) 输出数组的长度

 

在创建列表后,还有很多方法在列表尾部增加一个数据项(append()),或者从列表末尾删除书库(pop()),在列表末尾增加一个数据项集合(extend()).

name.append("amy") 增加一个末尾数据

name.pop() 删除末尾数据

name.extend(["amy", "lai"]) 末尾增加一个数据组

 

或者我们也可以再列表删除一个特定的数据项(remove()),或者在某个特定位置增加一个数据项(insert())。

name = ("alca",1990, "kalpana",1991,"arvin",1992)

在python中,我们可以直接混合类型的数据。

处理列表数据 我们可以使用for循环来进行任意大小的列表

for 目标标示符 in  列表 :

列表处理代码

如:

for item in name:

print(item)

 

或者我们使用while也可以:

count=0

while count<len(name):

print(name)

count=count+1

 

 

python分为列表、字典、元组。

 

在列表中存储列表

储存列表其实就是嵌套,在列表中嵌套多个列表:

name = [ "alca",1990, "kalpana",1991,"arvin",1992 

        [ "amy" ,     

            ["lily","lucy","hanks"]]]

所有列表的结尾只需要用中括号来结束就行了。

python from __future__ import division

 

from __future__ import division
导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精确除法之后,"/"执行的是精确除法,如下所示:
---------------------------------------------------------------------------------------------

>>> 3/4
0
>>> from __future__ import division
>>> 3/4

0.75

--------------------------------------------------------------------------------------------

导入精确除法后,若要执行截断除法,可以使用"//"操作符:
--------------------------------------------------------------------------------------------
>>> 3//4
0
>>> 
--------------------------------------------------------------------------------------------
一些将来特征如下:
featureoptional inmandatory ineffect
nested_scopes 2.1.0b1 2.2 PEP 227Statically Nested Scopes
generators 2.2.0a1 2.3 PEP 255Simple Generators
division 2.2.0a2 3.0 PEP 238Changing the Division Operator
absolute_import 2.5.0a1 2.7 PEP 328Imports: Multi-Line and Absolute/Relative
with_statement 2.5.0a1 2.6 PEP 343The “with” Statement
print_function 2.6.0a2 3.0 PEP 3105Make print a function
unicode_literals 2.6.0a2 3.0 PEP 3112Bytes literals in Python 3000
PEP:Python Enhancement Proposals
可以在这个地方找到很多PEP:http://www.python.org/dev/peps/ 里面还能看到许多提议的动机
比如有division:
The current division (/) operator has an ambiguous meaning for numerical arguments:
it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex.
This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.

python -Qnew

input用法:
技术分享
版本3中把input和raw_input整合了。
if用法
技术分享技术分享
数学运算
技术分享
内建函数:
技术分享技术分享
round函数会把浮点数四舍五入为最接近的整数值。
技术分享
用import导入了模块,然后按照"模块.函数"的格式使用这个模块
技术分享
import命令的另一种形式:
技术分享
在使用了"from模块import函数"这种形式的import命令后,就可以直接使用函数,而不需要模块名做前缀。
可以使用变量来引用函数。如,通过foo = math.sqrt进行赋值,然后可以使用foo来计算平方根
技术分享
int函数/类型把参数转换成整数时会自动向下取整。
技术分享
处理虚数的函数cmath
技术分享
语句,建立.py文件
技术分享技术分享
技术分享技术分享
在unix下执行,那么只要把下面的内容放到脚本首行
#!/usr/bin/env/ python
实际运行脚本之前要加权限
chmod a+x hello.py
然后运行程序
hello.py或者./hello.py
技术分享技术分享
改代码,在最后加入input("Press <enter>")即可。
python用井号(#)注释代码。
当python打印出字符串的时候,是用单引号括起来的
技术分享
当要打印的字符串包含单引号的时候,整个字符串是以双引号呈现的
技术分享技术分享技术分享
当单引号包含单引号的时候,被包含的单引号要用转义字符
技术分享技术分享
拼接字符串
技术分享技术分享
也可以像加法一样拼接
技术分享技术分享技术分享技术分享技术分享技术分享
原始字符串
技术分享
以上代码我们期望使用print打印出C:\nowhere,可是我们却打印出换行,于是我们采取以下措施
使用转义字符
技术分享
但对于长路径,则需要很多反斜杠(3版本不需要)。
技术分享
Python中的普通字符串在内部是以8位的ASCII码形式存储的,而Unicode字符串则存储为16位Unicode字符
技术分享
在Python3.0中,所有字符串都是Unicode字符串。
技术分享











































第一课基础知识

原文:http://www.cnblogs.com/yangziwen0709/p/5013419.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!