You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learn It - convert from number data type to string
myName="Jack"# A stringmyAge=5# A number# To convert myAge to a string so we can make a longer string to display:print(myName+" : "+str(myAge))
Learn It - comparing values and comparators
Comparators check if a value is (or is not) equal to, greater than (or equal to), or less than (or equal to) another value.
The result of comparator operation is always a Boolean vale - True or False
Depending on the comparing outcome, the program then decide what to do next.
Python has the following comparators:
logical operators
Equality, ==
Not equal, !=
Greater Than, >
Less Than, <
Greater than or equal to, >=
Less than or equal to, <=
Boolean operators: and, or, not
and: evaluates to True when both clauses (expressions) are True
or: evaluates to True when either of the expressions are True
not: evaluates to True when the expression is False. Evaluates to False when the expression is True.
Example usages of comparators
hunger=TruenumberOfPizza=5drink="fizzyPop"ifnumberOfPizza>=3:
print("Have some pizza, since we have 3 or more")
else:
print("You cannot have any pizza")
ifhunger==TrueandnumberOfPizza>=1:
print("You are hungry, and there is at least one pizza")
else:
print("Either you are not hungry or the food is not pizza")
ifnotdrink=="fizzyPop":
print("No drink for me.")
#The above is same as:ifdrink!="fizzyPop":
print("No drink for me.")
Learn It - create an array of numbers or strings
# An array of numberstestScores= [98, 78, 65, 89, 75]
# An array of stringsnames= ['Jack', 'Kerry', 'Julia', 'Matthew', 'David']
Learn It - array index and get an item from an array
Each individual data pieces is called an item
Each item has a position number called index
For the following array, names, “Jack” is at index 0.
To get “Jack”, you use names[0]
# An array of stringsnames= ['Jack', 'Kerry', 'Julia', 'Matthew', 'David']
Learn It - swap the positions of two items in an array
In Python, to generate random numbers, you use a module call random
At the start of your program/code, you need to import the module random
exmaple 1: generate a random whole number from 0 up to a limit
importrandomrandomNumber=random.randrange(100)
# The above code picks a random number between 0 and 99randomNumber=random.randrange(2, 10, 2)
# The above code picks a random number from 2, 4, 6, 8 (goes up by 2)
exmaple 2: generate a random number between a range
importrandomrandomNumber=random.randint(1, 100)
# The above code picks a random whole number between 1 and 100randomNumber=random.randint(50, 100)
# The above code picks a random whole number between 50 and 100
example 3: pick a random item from a list
importrandomnames= ['Jack', 'Kerry', 'Julia', 'Matthew', 'David']
randomIndex=random.randrange(len(names))
print("The random picked name is: ", names[randomIndex])
Learn It - Find out of a number is a mulitple of other number
In maths, we know that if a number A is a multiple of number B, it means A/B will not have a remainder - the remainder is 0.
In Python, there is an operator for computing remainder conveniently. The operator is called modulo which is %
Example 1
A=40
B=5
A%B will output 0
Example 2
A = 12
B= 9
A%B will output 3 since 12 only contains one 9, the remainder is 12- 1 x 9=3
Example 3
A = 9
B= 12
A%B will output 9 since 9 contains zero 12, the remainder is 9 - 0 x 12 = 9
This remainder operator can be used to check if a number is even or odd
num = int(input("Give me a number: "))
if num % 2 == 0:
print("It's an even number")
else:
print("Not an even number")