Learning with python: Chapter3~4 Functions,Conditionals and Recursion


1. Call Functions

>>> betty=type("32")   # call function @type
>>> print betty

<type 'str'>
>>> id(betty)           # call function @id
56091456
>>> betty=3
>>> id(betty)

155577720
>>> int("32")        # call function @int
32
>>> int(3.99)
3
>>> int(-2.3)
-2
>>> float(32)
32.0
>>> float("3.1415926")
3.1415926000000001
>>> str(3.1415926)       # call function @str
'3.1415926'
>>> minute=58
>>> float(minute)/60

0.96666666666666667
>>> import math  # use math functions, should import math
>>> decibel=math.log10(17.0)
>>> decibel

1.2304489213782739
>>> angle=1.5
>>> height=math.sin(angle)
>>> height

0.99749498660405445
>>> degrees=45
>>> angle=degrees*2*math.pi/360.0
>>> math.sin(angle)

0.70710678118654746
>>> math.sqrt(2)/2.0
0.70710678118654757
>>> x=math.exp(math.log(10.0))
>>> x

10.000000000000002


2.Define Functions

The syntax for a function definition is

def NAME( LIST OF PARAMETERS ):
    STATEMENTS

Examples:

# define functions
print "===========def func==========="
def newLine():  # def func
	print

print "First Line."
newLine();
print "Second Line"

newLine();
def odd_even(x):
	if x%2==0:
		print x, "is even"
	else:
		print x, "is odd"

odd_even(10);
odd_even(11);


def comp(x,y):
	if x < y:  
	  print x, "is less than ",y   # incdent 2 space is OK, not must TAB
	elif x > y:
	  print x, "is greater than ",y
	else:
	  print x, "and", y, "are equal"

comp(1,2);
comp(3,1);
comp(1,1);

x = -100;
if 0 < x < 200:
  print "x is between 0~200"
else:
  print "x is not in scale"

# recursion
def countdown(n):
	if n == 0:
	  print "Blastoff!"
	else:
	  print n
	  countdown(n-1)

countdown(5);

# keyboard input
print "===========input==========="
# print "Please input:"
inputthing = raw_input("What is your name?");
print inputthing
speed=input("What is the train speed?");  # integer
print speed

Results:

===========def func===========
First Line.

Second Line

10 is even
11 is odd
1 is less than  2
3 is greater than  1
1 and 1 are equal
x is not in scale
5
4
3
2
1
Blastoff!
===========input===========
What is your name?xiahouzuoxin
xiahouzuoxin
What is the train speed?100
100


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值