Chapter 2b - Built-in Functions

extracted from Runestone Academy
Built in Functionsβ
Using + with strings.β
we know that when we use + with numbers, the program will give us the mathematic operation of the sum of the numbers. But what happens when we have words?
s1 = "Agent" # assign values to string 1 and string 2
s2 = "007"
print ("Using + with strings ")
print("String 1: " + s1) # print values of s1 and s2
print("String 2: " + s2)
print ("String 1 + String 2: " + s1 + s2) # concatenation of strings - Line 18
print("")
π§ͺ Try the code out!
Mini quiz β strings and +β
Strings and +
1 / 2What is this?
"Agent" + "007"
Pick an answer to see if you are right.
str(), min(), max()β
# Part 1
a = 12 # assign value to variable a
b = 3 # assign value to variable b
c = 4 # assign value to variable c
print ("a: " + str(a) + " b: " + str(b) + " c: " + str(c) ) # print values for a, b and c
print ("Minimum of a, b and c is " + str(min(a,b,c) ) ) # print minimum value among a, b and c # Line 8
print ("Maximum of a, b and c is "+ str(max(a, b, c) ) ) # print maximum value among a, b and c # Line 9
print("") # print an empty line
print("")
π§ͺ Try the code out!
int() Converting Words into Integersβ
# Part 3
a = 12 # assign value to variable a
s2 = "007"
print ("A string which holds a number can be changed to number data type like this:")
s2_as_a_num = int(s2) # typecasting a number into a string
print("sum of a and s2_as_a_num: " + str(s2_as_a_num + a)) # adding a typecasted number with another
π§ͺ Try the code out!
pow() and abs()β
a = 8
b = 2
c = -9
d = 9.9
print(pow(a,b)) # returns a raised to the power b
print(pow(d,0)) # returns d raised to the power of 0
print(abs(c)) # returns the absolute value of c
print(abs(d)) # returns the absolute value of d
π§ͺ Try the code out!
Mini quiz β built-insβ
Built-in functions
1 / 2What is this?
int("007")
Pick an answer to see if you are right.
Exerciseβ
The long apple story is now a short numbers exam in Chapter 2a. Here, practice abs and min.
Activity: Absolute value
Write magnitude(n) so it returns abs(n) (you may call abs).