Materi Belajar Bahasa Python
Materi Belajar Bahasa Python
Python Comments
No. Comments character
1. #This is a comment #
2. """ This is a comment """
written in more that just one line""" Use a multiline string to make
the a multiline comment
Python Syntax
No. Syntax
1. Print ("Hello World")
Python Variabel
No. Syntax Variabel value
1. Carname = “Volvo “ Create a variable named
carname and assign the value
Volvo to it.
2. X = 50 50
3. Display the sum of 5 + 10,
X = 5
using two variables: x and y.
y = 10
15
print(x + y)
4. x = 5 Create a variable called z,
y = 10 assign x + y to it, and display
z = x + y the result.
print (z)
5. myfirst_name = "John" Remove the illegal characters
in the variable name:
6. X = Y = Z = "Orange" Insert the correct syntax to
assign the same value to all
three variables in one code
line.
7. def myfunc():
global x
x = "fantastic"
Python Number
No Syntax Value
.
1. x = 5 Insert the correct syntax to
convert x into a floating point
x = foat (x) number.
2. x = 5.5 Insert the correct syntax to
convert x into a integer.
x = int (x)
Python String
No Syntax Value
.
1. txt = "Hello World" Get the first character of
the string txt.
x = txt [0]
2. txt = "Hello World" Get the characters from index
2 to index 4 (llo).
x = txt [2:5]
3. txt = " Hello World " Return the string without any
whitespace at the beginning
x = txt.strip () or the end.
print (txt.format(age))
Python Boolean
No Syntax Value
.
1. print(10 > 9) The statement below would
print a Boolean value, which
True one?
2. print(10 == 9) The statement below would
print a Boolean value, which
False one?
Python Operator
No Syntax Value
.
1. print(10 * 5) Multiply 10 with 5, and print
the result.
2. print(10 / 2) Divide 10 by 2, and print the
result.
3. fruits = ["apple", "banana"] Use the correct membership
operator to check if "apple" is
if "apple" in fruits: present in the fruits object.
Python List
No. Syntax Value
1. fruits = ["apple", "banana", "cherry"] Print the second item in
the fruits list.
print(fruits [1])
2. Fruits [0] = “kiwi” Change the value from "apple"
to "kiwi", in the fruits list.
3. fruits = ["apple", "banana", "cherry"] Use the append method to
add "orange" to the fruits list.
fruits.append (“orange”)
4. fruits = ["apple", "banana", "cherry"] Use the insert method to add
"lemon" as the second item in
fruits.insert (1, “lemon’) the fruits list.
5. fruits = ["apple", "banana", "cherry"] Use the remove method to
remove "banana" from
fruits.remove (“banana”) the fruits list.
6. fruits = ["apple", "banana", "cherry"] Use negative indexing to print
the last item in the list.
print (fruits [-1])
7. fruits = ["apple", "banana", "cherry", Use a range of indexes to print
"orange", "kiwi", "melon", the third, fourth, and fifth item
"mango"] in the list.
print(fruits[2:5])
8. fruits = ["apple", "banana", "cherry"] Use the correct syntax to print
the number of items in the list.
print(len(fruits))
Python Tuples
No. Syntax Value
1. fruits = ("apple", "banana", "cherry") Use the correct syntax to print
the first item in
print( fruits [0]) the fruits tuple.
Python Sets
No. Syntax Value
1. fruits = {"apple", "banana", "cherry"} Check if "apple" is present in
the fruits set.
if "apple" in fruits:
fruits.update (more_fruits)
4. fruits = {"apple", "banana", "cherry"} Use the remove method to
remove "banana" from
fruits.remove (“banana”) the fruits set.
5. fruits = {"apple", "banana", "cherry"} Use the discard method to
remove "banana" from
fruits.discard (“banana”) the fruits set.
Python Dictionaries
No Syntax Value
.
1. car = { Use the get method to print
the value of the "model" key
"brand": "Ford", of the car dictionary.
"model": "Mustang",
"year": 1964
print(car.get (“model”))
2. car = { Change the "year" value from
1964 to 2020.
"brand": "Ford",
"model": "Mustang",
"year": 1964
car[“year”] = 2020
3. car = { Add the key/value pair
"color" : "red" to
"brand": "Ford", the car dictionary.
"model": "Mustang",
"year": 1964
Car.pop (“model”)
5. car = { Use the clear method to
empty the car dictionary.
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
Python if….else
No. Syntax Value
1. a = 50 Print "Hello World" if a is
b = 10 greater than b.
if a > b:
if a != b:
if a == b:
print (“yes”)
else:
print (“No”)
print (“1”)
elif a > b:
print (“2”)
else:
print (“3”)
5. if a == b and c == d: Print "Hello" if a is equal to b,
and c is equal to d.
print (“Hello Word”)
Print "Hello" if a is equal to b,
if a == b or c == d: or if c is equal to d.
print (i)
i += 1
while i < 6:
if i == 3:
break
i += 1
i += 1
if i == 3:
continue
print(i)
print(i)
i += 1
else:
print(x)
2. fruits = ["apple", "banana", "cherry"] In the loop, when the item
value is "banana", jump
for x in fruits: directly to the next item.
if x == "banana":
continue
print(x)
if x == "banana":
break
print(x)
Python Functions
No. Syntax Value
1. def my_function(): Create a function
named my_function.
print("Hello from a function")
my_function()
3. def my_function(fname, lname): Inside a function with two
parameters, print the first
print(fname) parameter.
4. def my_function(x): Let the function return
the x parameter + 5.
return x + 5
Python Lambda
No. Syntax Value
1. Create a lambda function that
x = lambda a:a takes one parameter (a) and
returns it.
Python Classes
No. Syntax Value
1. class MyClass: Create a class named MyClass:
x = 5
2. class MyClass: Create an object of MyClass
x = 5 called p1:
p1 = MyClass()
3. class MyClass: Use the p1 object to print the
x = 5 value of x:
p1 = MyClass()
print(p1.x)
Python Inheritance
No. Syntax Value
1. class Student (person): What is the correct syntax to
create a class
named Student that will
inherit properties and
methods from a class
named Person?
2. class Person: We have used
def __init__(self, fname): the Student class to create an
self.firstname = fname object named x.
What is the correct syntax to
def printname(self): execute
print(self.firstname) the printname method of the
object x?
class Student(Person):
pass
x = Student("Mike")
x.printname()
Python Modules
No. Syntax Value
1. Import mymodule What is the correct syntax to
import a module named
"mymodule"?
2. import mymodule as mx
If you want to refer to a
module by using a
different name, you can
create an alias.