python 系统学习笔记(一)

目标:熟悉python语言,以及学会python的编码方式。

如果你在window下, 去下载  http://www.python.org/getit/
安装起来, 然后运行python, 进入python解释环境。
如果你在ubuntu下, 执行: sudo apt-get install python, 然后在命令行下运行python, 进入python解释环境。

开始学习python

我建议你学习的过程也按照上面来,首先过一遍python官方文档:
如何查找python的某个功能?

1.写第一个Helloworld
当你学习一种新的编程语言的时候,你编写运行的第一个程序通常都是“Hello
World”程序,这已经成为一种传统了
在命令行的shell提示符下键入python,启动解释器。现在输入print 'Hello World',然后
按Enter键。你应该可以看到输出的单词Hello World 。(python 2.7  3.3目前已经是函数了)

     如何编写Python程序
下面是保存和运行Python程序的标准流程。
   1. 打开你最喜欢的编辑器。
   2. 输入例子中的程序代码。
   3. 用注释中给出的文件名把它保存为一个文件。我按照惯例把所有的Python程序都以
扩展名.py 保存。
   4. 运行解释器命令python  program.py或者使用IDLE运行程序。

2.运算符

反复练习!!

Python运算符列表

运算符

描述

x+yx-y

加、减,“+”号可重载为连接符

x*yx**yx/yx%y

相乘、求平方、相除、求余,“*”号可重载为重复,“%”号可重载为格式化

< span>< span><<===<>!=

比较运算符

+=-=*=/=%=**=< span><<=&=^=|=

自变运算符

x|y

按位或

x^y

按位异或

x&y

按位与

~x

按位取反

x< span>x<<y

x向左或向右移y

is, is not

等同测试

in, not in

是否为成员测试

orandnot

逻辑运算符

x[i]x[i:j]x.yx(...)

索引,分片,限定引用,函数调用

(...)[...]{...}'...'

元组,列表,字典,转化为字符串

 运算符优先顺序

运算符优先顺序列表(从最高到最低)

运算符

描述

'expr'

字符串转换

{key:expr,...}

字典

[expr1,expr2...]

列表

(expr1,expr2,...)

元组

function(expr,...)

函数调用

x[index:index]

切片

x[index]

下标索引取值

x.attribute

属性引用

~x

按位取反

+x-x

正,负

x**y

x*yx/yx%y

乘,除,取模

x+yx-y

加,减

x< y>x<<y

移位

x&y

按位与

x^y

按位异或

x|y

按位或

xx< yspan>x==yx!=yx<=yx<y

比较

x is yx is not y

等同测试

x in yx not in y

成员判断

not x

逻辑否

x and y

逻辑与

x or y

逻辑或

lambda arg,...:expr

Lambda匿名函数




3. if 语句
1. if else语句
2. if...elif...elif ..else
3.     if语句的嵌套

编写条件语句时,应该尽量避免使用嵌套语句。嵌套语句不便于阅读,而且可能会忽略一些可能性。

练习
#score = raw_input("score:")
#score=int(score)
score=85


# if else   demo1
if( score >60):
    print 'pass'
else:
    print 'fail'


# if elif else demo2
if(score> 90):
    print 'A'
elif(score >80) and(score <90):
    print 'B'
elif(score>70) and (score<80):
    print 'C'
else:
    print 'D'


#if include if  demo3


a=3;
b=4;
c=5;
if(a>b):
    if(c>a):
        print 'Max is c'
    else:
        print 'Max is a'
else:
    if(b>c):
        print 'Max is b'
    else:
        print 'Max is c'
print 'done'

        
 
4.for 语句

1、一般格式
Python for循环的首行定义了一个赋值目标(或【一些目标】),以及你想遍历的对象,首行后面是你想重复的语句块(一般都有缩进)
for <target> in <object>:
    <statements>
else:
    <statements>
当ptyhon运行for循环时,会逐个将序列对象中的元素赋值给目标,然后为每个元素执行循环主体。循环主体一般使用赋值的目标来引用序列中当前的元素,就好像那事遍历序列的游标。

for首行中用作赋值目标的变量名通常是for语句所在作用于的变量(可能是新的)。这个变量名没有什么特别的,甚至可以在循环主体中修改。但是当控制权再次回到循环顶端时,就会自动被设成序列的下一个元素。循环之后,这个变量一般都还是引用了最近所用过的元素,也就是序列中最后的元素,除非通过一个 break语句退出了循环。

for语句也支持一个选用的else块,它的工作就像在while循环中一样:如果循环离开时没有碰到break语句,就会执行(也就是序列所有元素都被访问过了)
break和continue语句也可用在for循环中,就像while循环那样。for循环完整的格式如下:
for <target> in <object>:
    <statements>
    if <test>:break
    if <test>:conitnue
else:
    <statements>


a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']
    
# will iterate 3 times,
# the last iteration, b will be None
print "Map:"
for x, y in map(None, a, b):
  print x, y
    
# will iterate 2 times,
# the third value of a will not be used
print "Zip:"
for x, y in zip(a, b):
  print x, y
    
# will iterate 6 times,
# it will iterate over each b, for each a
# producing a slightly different outpu
print "List:"
for x, y in [(x,y) for x in a for y in b]:
    print x, y  


#demo for 'for'
# -*- coding: cp936 -*-


#for in
for i in range(1,5):
    print i
    
#step 2
for i in range(1,5,2):
    print i;


#break
for i in range(1,5):
    if(i==6):
        break 
else:
    print 'break hello'


#求质数
import math  
for i in range(50, 100 + 1):
    for j in range(2, int(math.sqrt(i)) + 1):
        if i % j == 0:
            break
    else:
        print i
        


#continue
for i in range(1,5):
    if(i==4):
        continue
    print 'no met continue'
else:
    print i
    


 


5.while 语句
while循环的一般格式如下:
while <test>:
    <statements1>
    if <test2>:break
    if <test3>:continue
    if <test4>:pass
else:
    <statements2>
break和continue可以出现在while(或for)循环主体的任何地方,但通常会进一步嵌套在if语句中,根据某些条件来采取对应的操作。

#demo for while
a=4;
while (a>0):
    print a;
    a=a-1;
    if(a==1):
        break
else:
    print 'no meet break'
    
 #continue
a=4;
while (a>0):
    print a;
    a=a-1;
    if(a==1):
        continue
    print 'no meet continue'



 
下回介绍。。。 python string 处理

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值