Python Programming - Mathematics, Computer Science & Physics ...

15 downloads 2475 Views 440KB Size Report
Python Programming, 1/e. 1. Python Programming: An Introduction to. Computer Science. Chapter 2. Writing Simple Programs. Python Programming, 1/e. 2.
! "

#

$ !

! $ $

%

$

"

$#

" $

%

'!

"

$ #

$#

* $

'!

+ ! (" (

(

&

"

$#

'

+

'!

)

"

#

#

$#

*

( ' ’ # # % # #

# #

" # % .

# (

'!

! ( #

$

# $ ( !

# $

(

,

(

-

"

$#

'!

"

"

$#

'!

" $

( # #

$ # % ( 1 $+ ( "

(

2

( . !

$ “

% ”

3 /

"

$#

'!

0

6+ "

! –

! #

! $ # $

( !



+

*

(

– 7 4 ,1

2 8&

$

!! !



(

*

4

6+ "

5

6+ "

!

!

9$

' 1 $

6

2 ! *7 4 ,12 8 &

’ #

# $ $

2 1

#

*

( :

# (

6+ "

6+ "

!

!

#convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell

1

2 $ $

1 4 ,2;

< ##

def main(): celsius = input("What is the Celsius temperature? ") fahrenheit = (9.0/5.0) * celsius + 32 print "The temperature is ",fahrenheit," degrees Fahrenheit."

8&

!

3

main()

&

6+ "

! # # 3

6 #

>>> What is the Celsius The temperature is >>> main() What is the Celsius The temperature is >>> main() What is the Celsius The temperature is >>>

)

$

< < $ " 6!

temperature? 0 32.0 degrees Fahrenheit. temperature? 100 212.0 degrees Fahrenheit.

! 2

1 ! 2

$

# ( !(

,

6

$ "

$ $

-

6

$

!

$ $

"

?

#

$ % #

$ (

!

("

!

$

$ (

!

( $

= =

(

$ $ 1 “=”2 $ # $

>

temperature? -40 -40.0 degrees Fahrenheit.

! 1

$

(

=6 =6

*

/

(

0

&

6

$

6 >>> >>> 5 >>> 5 >>>

6+ " $

$ # (

+ @ !

!

(5 +

x = 5 x print x print spam

Traceback (most recent call last): File "", line 1, in -toplevelprint spam NameError: name 'spam' is not defined >>>

$ ( ( &(4 $

$

(

< !

6

# #

!

(

4

6

5

$

6

+ ( 8 A ; ;;

$ $ +

!

#

+

(

!

# ( #

" ( 1 1 + – +2

(

; 2 81

%;;&2

%

$

# !

6

( “” +

(

$

print 3+4 print 3, 4, 3+4 print print 3, 4, print 3+ 4 print “The answer is”, 3+4

7 3 4 7

B! C 7B + C ! $ + + " + DE ! # # ! @E (

3 4 7 The answer is 7

&

!

)

)

x = 3.9 * x * (1-x) fahrenheit = 9.0/5.0 * celsius + 32 x = 5

F # >>> >>> 0 >>> >>> 7 >>> >>> 8 >>>

3

myVar = 0 myVar myVar = 7 myVar myVar = myVar + 1 myVar

,

F !

%

-

"

+#

$ $

( !

! #

#

( “



(

1 + 2( ! % ” +”(

% !

/

"

* " ! "

$

$ ! ( B! C7 1 B 6( ( +7 1 “6 ”2

0

C2

! $

# B +

C #

! !

(

>>> def inp(): x = input("Enter something ") print x

>>> inp() Enter something 3+4 7 4

&5

,

!

!

$ $7 +8 +A

B! C B! C … 7 B + C B + C … 6! + DE ! @E

E# ! $ +

# G



# % G

+7 7+

!

&

# >

! + 7 >>> >>> >>> 3 4 >>> >>> 4 3

!

&

$#

! 3

3

+



$

:

x = 3 y = 4 print x, y

>>> def spamneggs(): spam, eggs = input("Enter the number of slices of spam followed by the number of eggs: ") print "You ordered", eggs, "eggs and", spam, "slices of spam. Yum!“

x, y = y, x print x, y

>>> spamneggs() Enter the number of slices of spam followed by the number of eggs: 3, 2 You ordered 2 eggs and 3 slices of spam. Yum! >>>

&&

'$

@

$ $

&)

'$ + ( (

# $ B! C B > B C "

$ % # ( C

$ B! C B C " !

+

!

@ B >

+( >

$ % (

C

$ !

$ ( &,

&-

-

'$

@

'$

@

>>> for i in [0,1,2,3]: print i

(

152

#

G

>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0 1 2 3 >>> for odd in [1, 3, 5, 7]: print odd*odd

D 5( D

1 9 25 49 >>>

A $

$ #

152 # % + 5 (

$

&/

'$

&0

6+ F

@ $# $ $

+

*

H

%

(

( E#

# $

#

5

#G !

$

!

5

&4

6+ F

*

)5

6+ F

* *

$ :

!

F "

$

!

:

" +

"

$

( " $

%

… 5 )

!

F ;1 8 (

$ $ 2( "

!

5 ! )

/

6+ F

*

6+ F

*

' $ D

1 1 2

6 1

2

“ “

5 7 !

;1 8

$ 2 $ ”

!"#

2

$ %

$

& “'

(”)

)&

6+ F

*

%

D

& “'

*

# futval.py # A program to compute the value of an investment # carried 10 years into the future

(”)

def main(): print "This program calculates the future value of a 10-year investment."

& !")(

7 %

*& !+

!

;1 8

2

!"

principal = input("Enter the initial principal: ") apr = input("Enter the annual interest rate: ") for i in range(10): principal = principal * (1 + apr)

)

$

print "The value in 10 years is:", principal

$5

main()

( ”,

),

6+ F

))

6+ F

5





)-

*

>>> main() This program calculates the future value of a 10-year investment. Enter the initial principal: 100 Enter the annual interest rate: .03 The value in 10 years is: 134.391637934 >>> main() This program calculates the future value of a 10-year investment. Enter the initial principal: 100 Enter the annual interest rate: .10 The value in 10 years is: 259.37424601

)/

0