
The Predefined Functions:
| Created | |
|---|---|
| Tags |
There are several predefined functions in python. Many were covered in National 5, except for chr() and ord().
A predefined function is built in to the software, so you do not need to manually program it.
- Think about how you would actually program random.randint(1,100) if you had no random module. You would have to waste tons of lines creating randomness by yourself instead of using just one.
1. int(x):
number = float(input("Enter your float number. For example, 4.87: ")) #asks for input
number = int(number) #convert into integer
print(number) #just prints the integer value. so it becomes 4.2. float(x):
number = int(input("Enter your integer number. For example, 4: ")) #asks for integer
number = float(number) #converts into float
print(number) #prints 4.0, for example.3. round(x,y)
Rounding is an essential feature in python, so whenever you need to provide an output for a user, sometimes it can be useless to get 12 decimal places when one is needed.
- The x-value is the number being rounded.
- The y-value is the number of decimal places (d.p.) it is being rounded to.
number = 8.1243124312431243 #a long, unnecessary float.
number = round(number,3)
print(number)format(x,y)
- This is simply a way of formatting numbers in the way you want it to be.
number = 2.718281828 #a float to round.
print(format(number,".2f")) #rounds it to 2 decimal places.- You can also use this for strings:
name = "John"
age = "18"
place = "Scotland"
print("Hello, my name is {}, I am {} years old, and I live in {}".format(name, age, place))
#the curly brackets are placeholders which are filled with the details in format.len(x)
- Len is the length of a string or an array.
name = "Jonathan"
array = [1,2,3,4,5,6,7,8,9]
print(len(array)) #returns 9.
print(len(name)) #returns 8.Random:
- This finds a random integer, from a certain range. Remember to import the random module for this to work.
import random #remember to i
number = random.randint(1,100) #creates a random integer from 1 to 100 ad assigns it to "number".
array = [1,2,3,4,8,9]
selected = random.choice[array] #selects the random element from the array.
Chr() vs Ord()
- Two very similar predefined functions, where the purpose is to convert from ASCII code to character (not necessarily letter. ASCII can do space and punctuation as well.) and vice versa.
lettertoascii = ord("a") #97.
asciitoletter = chr(97) #aSubstrings:
- This takes a string and only returns the parts you need.
- For example, “Hello World” has a substring of “Hello”.
phrase = "Computing Science"
print(phrase[0]) #returns C
print(phrase[6] == phrase[12]) #returns True because both are "i".
print(phrase[0:5]) #returns "Compu"
print(phrase[-4] #returns "e", the fourth from the end.Modulo:
Modulus is represented by the percentage sign (%) and only prints the remainder of a division function.
dividend = int(input("Enter your first number: "))
divisor = int(input("Enter your second number: please make it smaller. "))
remainder = dividend % divisor
print(remainder) #prints the output of the modulus operation.