0% found this document useful (0 votes)
6 views

02-Functions 4pp

The document discusses algorithms and their translation to computer programs using Python. It covers variables, data types, expressions, operators, functions, and passing/returning values from functions. Examples are provided to demonstrate various Python concepts.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

02-Functions 4pp

The document discusses algorithms and their translation to computer programs using Python. It covers variables, data types, expressions, operators, functions, and passing/returning values from functions. Examples are provided to demonstrate various Python concepts.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Algorithm Python

A precise sequence of simple steps to solve a problem translating an algorithm into a computer program
Type: int Type: oating-point
meaning_of_life = 42

a = 6.02
print( meaning_of_life )

output: 42
fl
Type: string Type: string
last_letter = "z" print( "hello" )

print( last_letter ) output: hello

output: z
hello = 5

print( hello )

output: 5

Type: string Type: string


print( "4 + 7" ) output: 4 + 7 print( 4 + 7 )

print( 4 + 7 ) output: 11 output: 11

print( "hello " + "my name" )

output: hello my name


Type: conversion Type: boolean

print( float(4) ) 4.0

print( int(3.14) ) 3
x = True # not same as x = “True”

print( str(4) + str(2) ) 42


y = False # not same as y = “False”

print( int("4") + int("2") ) 6

Type: functions Type: functions

max(3,4) —> 4 min(3,4) —> 3

f = max min = max


f(3,4) —> 4 min(3,4) —> 4
Expressions and Operators Summary
addition +
• Variables
subtraction - • store information in computer memory
multiplication * • int, oat, string, booleans, functions

division / 4/3 -> 1.3333333333333333 • Expressions and Operators


• arithmetic
int division // 4//3 -> 1 • similar to functions
• assignment
exponentiation pow pow(2,3) -> 8
modulus (mod) % 9 % 4 -> 1

a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) d = "c: " + str(c) c: 8

b = 30
a = b
fl
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) c: 8 d = "c: " + str(c) c: 8

b = 30 b = 30
a = b a —> 30 a = b a —> 30

print(e) print(e) error

4 = a

a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) c: 8 c = "hello"
print( b + c )
b = 30
a = b a —> 30

print(e) error

4 = a error
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
c = "hello" c = "hello"
print( b + c ) error print( b + c ) error

print( ??? ) 3 hello

a = 5 Practice Passing Values


b = 3
c = a + b
c = "hello"
print( b + c ) error

print(str(b) + " " + c )) 3 hello


Returning Values Returning Values
# the function sqrt takes as input a number and returns a number
from math import sqrt def compute_four():
x = sqrt(4) return 24 / 4 - 2
# the operator "+" takes as input two numbers and returns a number
x = 8 + 12
x = compute_four() 4

# the function len takes as input a string and returns an integer y = 24 / compute_four() 6
x = len("eggplant")
print( compute_four() ) 4
# the return value of one function can be the input to another
x = int(8.485) + 12
x = int(sqrt(72)) + 12

Passing & Returning Passing & Returning


def add_five(x):
x = x + 5
return x
def return_two_things(x,y):
z = 4
return(x+y,x*y)
add_five(z)
print(z) 4 (s,p) = return_two_things(2,5)

x = add_five(z)
print(x) 9

print(add_five(z)) 9
Passing & Returning

def return_two_things(x,y):
return(x+y,x*y)
print(x,y)

(s,p) = return_two_things(2,5)

You might also like