0% found this document useful (0 votes)
115 views12 pages

Materi Belajar Bahasa Python

The document shows examples of assigning variables of different data types in Python like integer, string, float, list, tuple, and dictionary. It also demonstrates printing the type of each variable to show the data type. Some examples include assigning an integer x = 5, a string x = "Hello World", a float x = 20.5, a list x = ["apple", "banana", "cherry"], a tuple x = ("apple", "banana", "cherry"), and a dictionary x = {"name": "John", "age": 36}. Each example prints the type of x to display the variable's data type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views12 pages

Materi Belajar Bahasa Python

The document shows examples of assigning variables of different data types in Python like integer, string, float, list, tuple, and dictionary. It also demonstrates printing the type of each variable to show the data type. Some examples include assigning an integer x = 5, a string x = "Hello World", a float x = 20.5, a list x = ["apple", "banana", "cherry"], a tuple x = ("apple", "banana", "cherry"), and a dictionary x = {"name": "John", "age": 36}. Each example prints the type of x to display the variable's data type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

No.

Syntax Data Type of x


1. x = 5 int
print(type(x))
2. x = "Hello World" str
print(type(x))
3. x = 20.5 float
print(type(x))
4. x = ["apple", "banana", "cherry"] list
print(type(x))
5. x = ("apple", "banana", "cherry") tuple
print(type(x))
6. x = {"name" : "John", "age" : 36} dict
print(type(x))
7. x = True bool
print(type(x))
Exercise

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")

2. Insert the missing indentation


if 5 > 2:
to make the code correct:
print("Five is greater than two!")

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)

3. x = 5 Insert the correct syntax to


convert x into a complex
x = complex (x) number.
4. x = "Hello World" Use the len method to print
the length of the string.
print (len (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.

4. txt = "Hello World" Convert the value of txt to


upper case.
txt = txt.upper()
5. txt = "Hello World" Convert the value of txt to
lower case.
txt = txt.lower()
6. txt = "Hello World" Replace the character H with
a J.
txt = txt.replace(“H”,” J”)
7. age = 36 Insert the correct syntax to
add a placeholder for the age
txt = "My name is John, and I am {} parameter.

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?

3. print(10 < 9) The statement below would


print a Boolean value, which
False one?
4. print(bool("abc")) The statement below would
print a Boolean value, which
True one?
5. print(bool(0)) 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.

print("Yes, apple is a fruit!")

4. if 5 != 10: Use the correct comparison


operator to check if 5 is not
print("5 and 10 is not equal") equal to 10.
5. if 5 == 10 or 4 == 4: Use the correct logical
operator to check if at least
print("At least one of the statements is one of two statements is True.
true")

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.

2. fruits = ("apple", "banana", "cherry") Use the correct syntax to print


print (len(fruits)) the number of items in
the fruits tuple.
3. fruits = ("apple", "banana", "cherry") Use negative indexing to print
the last item in the tuple.
print(fruits [-1])

4. fruits = ("apple", "banana", "cherry", Use a range of indexes to print


"orange", "kiwi", "melon", "mango") the third, fourth, and fifth item
in the tuple.
print(fruits[2:5])

Python Sets
No. Syntax Value
1. fruits = {"apple", "banana", "cherry"} Check if "apple" is present in
the fruits set.
if "apple" in fruits:

print("Yes, apple is a fruit!")


2. fruits = {"apple", "banana", "cherry"} Use the add method to add
"orange" to the fruits set.
fruits.add (“orange”)
3. fruits = {"apple", "banana", "cherry"} Use the correct method to add
multiple items (more_fruits) to
more_fruits = ["orange", "mango", the fruits set.
"grapes"]

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 [“color”] = “red”


4. car = { Use the pop method to
remove "model" from
"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:

print ("Hello World")


2. a = 50 Print "Hello World" if a is not
b = 10 equal to b.

if a != b:

print ("Hello World")


3. a = 50 Print "Yes" if a is equal to b,
b = 10 otherwise print "No".

if a == b:

print (“yes”)

else:

print (“No”)

4. a = 50 Print "1" if a is equal to b, print


b = 10 "2" if a is greater than b,
otherwise print "3".
if a == b:

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 (“Hello Word”)

6. This example misses


if 5 > 2: indentations to be correct.
Insert the missing indentation
print("Five is greater than two!") to make the code correct:

7. Use the correct short hand


syntax to put the following
if 5 > 2: print("Five is greater than statement on one line:
two!")

8. Use the correct short hand


syntax to write the following
print("Yes") if 5 > 2 else print("No")
conditional expression in one
line:

Python While loops


No. Syntax Value
1. i = 1 Print i as long as i is less than
6.
while i < 6:

print (i)

i += 1

2. i = 1 Stop the loop if i is 3.

while i < 6:

if i == 3:

break

i += 1

3. i = 0 In the loop, when i is 3, jump


directly to the next iteration.
while i < 6:

i += 1

if i == 3:

continue
print(i)

4. i = 1 Print a message once


the condition is false.
while i < 6:

print(i)

i += 1

else:

print("i is no longer less than 6")

Python For loops


No. Syntax Value
1. fruits = ["apple", "banana", "cherry"] Loop through the items in
the fruits list.
for x in fruit:

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)

3. for x in range(6): Use the range function to loop


through a code set 6 times.
print(x)

4. fruits = ["apple", "banana", "cherry"] Exit the loop when x is


"banana".
for x in fruits:

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")

2. def my_function(): Execute 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

5. If you do not know the number


of arguments that will be
def my_function(*kids):
passed into your function,
there is a prefix you can add in
print("The youngest child is " + kids[2])
the function definition, which
prefix?
6. If you do not know the number
of keyword arguments that will
def my_function (**kids):
be passed into your function,
there is a prefix you can add in
print("His last name is " + kid["lname"])
the function definition, which
prefix?

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)

4. class Person: What is the correct syntax to


def (__init__)(self, name, age): assign a "init" function to a
self.name = name class?
self.age = age

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.

What is the correct


syntax for creating an
alias for a module?

3. import mymodule What is the correct syntax of


printing all variables and
print(dir(mymodule)) function names of the
"mymodule" module?

4. from mymodule import person1 What is the correct syntax of


importing only the person1
dictionary of the "mymodule"
module?

You might also like