Python Basics Tutorial
Python Basics Tutorial
Then answer all the following questions. You can run the codes either on your laptop (if you
have already installed Python 3) or you can run the Python Online Compiler to test and verify
your answers at:
https://repl.it/languages/python3
Copy each of your answers and paste into this word document. Also paste your output
screenshots here.
List/Array
5. How do you create a list/array that consists of 5 different numbers?
6. How do you iterate over an array of string e.g. [“one”, “two”, “three”] and print each
element?
7. How do you concatenate two arrays of strings and print the result?
8. How do you duplicate elements in an array?
9. How do you print the size of an array?
10. How do you print the first until the fourth element of an array?
1
11. How do you count how many occurrences of a value e.g. “one” in an array?
String
12. Create a string variable with a value of “Hello, good morning!” and print
13. Print the same variable by skipping 1 character as a full string
14. Print from the string only from the third character until the end
If/Else
15. Create a variable with an int value. If the value is greater than 10, print “Greater than 10”
message, else print “Equal or less than 10”
16. Create an array of string with three elements, e.g. “Monday”, “Tuesday”, “Wednesday”. If
the length of the second string in the array is greater than 6, print “string longer than 6
chars”
Function
17. How do you define a function that multiplies two numbers?
18. How do you call the multiply function (above) and print the result?
Class
19. How do you create a Class of Food that can be assigned a name, type (e.g. western, malay
etc) and price?
20. How do you add a function eat() to the Class Food (above) that prints different user’s
expression (“Yummy”, “Yucky”, etc) when eating the food based on the different types?
21. How do you create 3 instances of class Food and define their different properties?
22. How do you invoke the function of each class instances that print different expression
once eaten?
Dictionary
23. How do you create a dictionary object that stores different foods with their prices?
24. How do you add a new food to the dictionary?
25. How do you add a remove a food from the dictionary?