Beginning Python - Chapter5 : conditinals,loops,and some other statements
来源:互联网 发布:秀米同类软件 编辑:程序博客网 时间:2024/06/15 02:19
#Chapter5:conditinals,loops,and some other statements
# Questions:about namespace with exec
#1 more about print & import
# -1.1- print
print 'hello', \
'world' #same with
print 'hello',
print 'world'
# -1.2- import
# import somemodule
# from somemodule import somefuction
# from somemodule import *
# import somemodule as anothername
# from m1 import open as open1
# from m2 import open as open2
import math as footbar
print footbar.sqrt(4) #2.0
#2 assignment magic
# -2.1- sequence unpacking
x,y,z = 1,2,3
print x,y,z # 1 2 3
x,y = y,x
print x,y,z # 2 1 3
v=1,2,3
print v #(1, 2, 3)
dict = {'name':'lili','no':'01'}
key,value = dict.popitem()
print key,value # name lili
# x,y = 1,2,3 or x,y,z=1,2 wrong ,but can in python3.0
# -2.2- chained assignments
x = y = 1 # y=1 ; x=y
print x,y
# -2.3- augmented assignments
x=2
x+=1 # *,/,%
print x
string = 'hi'
string+='girl'
string*=2
print string #higirlhigirl
# -2.4- Blocks(块):the joy of indentation(缩进)
# same indentation is in the same block,with out {}
# a colon(:) is used to indicate that a block is about to begin
# -2.5- condition and condition statements
# -- all kinds of data type can be conversed to Boolean explicitly
# -- false : None 0 "" () [] {} ,but the have different return type
# -2.6- condition execution and the if statement
x = 0
if x>0:
print "x>0"
elif x<0:
print "x<0"
else:
print "x=0"
# -2.7- Nesting Blocks(块嵌套)
x = 3
if x>0:
if x==1:
print "x=1"
elif x==2:
print "x=2"
else: # else can not have condition
print "x=",x
#print 'another if'
if x==3:
print "xx=3"
# -2.8- Comparison operators
# cannot comparing imcompatible types after Python3.0
# x is y same object
# x is not y
# x in y a member of the container(sequence..)
# x not in y
if (1 or 0):print 'or' #or
if (1 and 0):print 'and'
if (1 and not 0):print 'and not' #and not
# name = raw_input('pls enter your name: ') or ('unknown')
# print name # if enter nothing,print unknown
# -2.9- assertions
age = -1
#excute condition is false
# assert 0 < age < 100,'the age must be realistic'
# -2.10- loops
x = 1
while x<10: # Question: cannot change to for
print x
x+=1
# ---- while loop
# while condition is ture ,excute
# have to input something but space if you want to leave this loop
#name = ''
#while not name or not name.isspace() or not name.strip():
# name = raw_input( 'pls enter your name : ')
#print 'hello,%s' %name
# ---- for loop
words = ['this','is','a','test']
for word in words:
print word # different with C , iterator
for num in range(0,2):
print num # 0 1
dict = {'x':1,'y':2}
for key in dict:
print key,',',dict[key]
for key,value in dict.items():
print key,',',value
# ---- some iteration utilities
# -- parallel(并行) iteration
# iter two(or more) seq
name = ['lili','lucy']
age = [12,13]
no = [01,02]
for i in range(len(name)):
print name[i],"'s age is",age[i]
# using zip
print zip(name,age) # [('lili', 12), ('lucy', 13)]
print zip(no,name,age) #[(1, 'lili', 12), (2, 'lucy', 13)]
# using zip for unpack
for n,a in zip(name,age):
print n,a
# range,xrange;xrange just caculate only those numbers needed
print zip(range(3),xrange(100)) #[(0, 0), (1, 1), (2, 2)]
# -- numbered iteration
# enumerate (列举)
strings =['1xx','2','3xx']
for index,string in enumerate(strings):
if 'xx' in string:
print index,string # 0 1xx 2 3xx
# -- Reversed and Sorted Iteration
print ''.join(reversed('hello!')) # !olleh
# -2.11- Breaking out of loops
# -- continue & break
for i in range(4):
if i == 1:
continue
print "always don't show"
if i == 2:
print i
break
if i == 3:
print "always don't show"
# -- The while True / using break
#while True:
# word = raw_input('enter your name: ')
# if not word:
# print 'you enter nothing'
# break
# print 'you name is',word
# -2.12-slightly loopy
print [x*x for x in range(3)] # must have [] ; [0, 1, 4]
print [x*x for x in range(3) if x%2==0] #[0, 4]
# -2.13- pass del exec
# -- pass : as a placeholder ,do nothing
i = 2
if i == 1 : print '1 is here'
elif i == 2 :
#not finish , below elif must have a statement
pass
print 'pass' # can be printed
# -- del
# delete only the name, not the list itself (the value).
# In fact, there is no way to delete values in Python -
# and you don’t really need to, because the Python interpreter does it
# by itself whenever you don’t use the value anymore.
x = 1
y = x
print x,y
x = 2
print x,y
x = None
print x,y
del x
# print x # wrong ,x is not defined
print y
# -- exec
# exec doesn’t return anything because it is a statement itself
exec "print 'hello,world'"
from math import sqrt
exec 'sqrt = 1'
# sqrt(4) wrong sqrt has been overwrited
from math import sqrt
scope={}
exec 'sqrt = 1' in scope
print eval('sqrt+sqrt',scope)
print 'sqrt not be overwrited', sqrt(4)
print scope['sqrt']
# -- eval (evaluate)
# like exec,Just as exec executes a series of Python statements, eval evaluates a Python expression (written in a string) and returns the resulting value
# exec raw_input('enter a arithmetic ex: ')
# print eval (raw_input('enter a arithmetic ex: '))
scope={}
scope['x']=3
scope['y']=4
print eval('x*y',scope)
- Beginning Python - Chapter5 : conditinals,loops,and some other statements
- Beginning Python Chapter5
- nba and some other games live website
- Using UNION, EXCEPT and INTERSECT with Other Transact-SQL Statements
- Python Tutorial :Loops and List Comprehensions 其他
- JavaScript_A Beginner's Guide - Conditional Statements and Loops - 09/23/2012
- python之三-statements and functions
- Some other questions
- Beginning Python - Chapter2 : Lists and Tuples
- while and for Loops--Learning Python-Chapter 13
- Learning with python: Chapter2 Variables, expressions and statements
- 【Some】【Other】100小时定律
- 【Some】【Other】程序员的忧虑
- Beginning Python
- Python 入门教程 13 ---- Loops
- Python--Loops,Unpacking,Lambda
- chapter5
- chapter5
- 在ABAP程序中调用Web Service
- asp.net配置篇之web.config详解
- 如何在HTML中画一条线
- png图片格式分析
- 自顶向下的归并排序
- Beginning Python - Chapter5 : conditinals,loops,and some other statements
- Prism:来自Facebook的真正的大数据项目
- 看看神棍的表演
- Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法
- Msexchange 复制服务无法启动,导致数据库无法加载!
- java 图片放大缩小
- 针对ARM的视频解码优化
- 制作游戏并非易事 开发者需扮演多种角色
- 入学以来大学生活总结