Tugas Pemrograman Python (13-11-2019)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

KERJAKAN TUGAS BERIKUT DAN DIKUMPUL VIA EMAIL SEBELUM JAM 24:00 13 NOVEMBER 2019

# Getting input from User

input("Enter your name: ")

input("Enter your age: ")

name = input("Enter your name: ")

age = input("Enter your age: ")

print("Hello " + name + "! your are " + age + " years old !")

# Building Basic Calculator

num1 = input("Enter a number: ") # enter a number

num2 = input("Enter a number: ") # enter another number

result = float(num1) + float(num2)

print(result) # What do you think! this default becomes a String

result = int(num1) + int(num2) # Integer number, what do you think?

result = float(num1) + float(num2) # Floating number: whole number

# Creating a List

friends = ["Kevin", "Karen", "Jim"] # index start from 0

print(friends)

# Accessing elements of list

print(friends[0]) # from index 0, first indexed

print(friends[-1]) # from index -1, last indexed

print(friends[1:]) # from index 1 to the last indexed

# List functions

lucky_number = [4, 8, 15, 16, 23, 42]

friends = ["Kevin", "Karen", "Jim", "Oscar", "Tom"]


#print(friends)

friendsriends.extend(lucky_number) # extending list

print(friends)

friends.append("Sugy") # Adding list

friends.insert(1, "Kelly") # Inserting list

friends.sort()

lucky_number.sort()

lucky_number.reverse()

print(friends)

print(lucky_number)

# Tuple basically same as list, tuple used for store data that could not be changed

# The coordinate data

coordinate = [4, 5]

print (coordinate)

print (coordinate [0])

print (coordinate [1])

# Working with function

# Definition of function

def say_hi () : # function without a parameter

print("Hello User")

# execution of function

say_hi()

def say_hi (name) : # function with parameter name

print("Hello " + name)

# execution of function

say_hi("John")
def say_hi (name, age) : # function with parameters name and age

print("Hello " + name + ", you are " + str(age))

# execution of function

say_hi("John", 35)

# Using return statement >>> untuk mengembalikan informasi sebuah fungsi

# For example

def cube(num) :

num*num*num

cube(3)

print(cube(3)) # nothing happen ?

def cube(num) :

return num*num*num

print(cube(3)) # return to 27

print(cube(4))

result = cube(4)

print(result)

You might also like