UTS Pengantar Python
UTS Pengantar Python
No Program Keluaran
1 def calc_q1(x):
q=4*x+1
return q
calc_q1(5)
print(q)
What output is produced by the print() statement when
the following code is executed?
2 def get_input():
x = float(input("Enter a number: "))
return x
def main():
get_input()
print(x ** 2)
main()
def inc_by_two(x):
x=x+2
return x
x = 10
inc_by_two(x)
print("x = ", x)
a=5
b = -10
if a < b or a < 0 and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")
KEMENTERIAN PENDIDIKAN DAN KEBUDAYAN
UNIVERSITAS SEBELAS MARET
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
PROGRAM STUDI INFORMATIKA
Jl. Ir. Sutami 36 A Kentingan Surakarta 57126 Telp/Fax. (0271) 66 3375
5 What is the output generated by the following code?
a = -5
b = -10
if (a < b or a < 0) and b < 0:
print("Yes, it’s true.")
else:
print("No, it’s false.")
6 What output is produced by the following code?
def monotonic(xlist):
for i in range(len(xlist) - 1):
if xlist[i] < xlist[i + 1]:
return False
return True
data1 = [5, 3, 2, 2, 0]
data2 = [5, 2, 3, 2, 0]
print(monotonic(data1), monotonic(data2))
7 What output is produced by the following code?
def swapper(xlist):
for i in range(len(xlist) - 1):
if xlist[i] > xlist[i + 1]:
# Swap values.
xlist[i], xlist[i + 1] = xlist[i + 1], xlist[i]
data = [5, 3, 2, 2, 0]
swapper(data)
print(data)
8 What is the value of x after the following code executes?
y = 10
if 2 * 4 - 8:
x=2*4-8
else:
x=y
9 To what value is the variable x set by the following
code?
def multiply_list(start, stop):
product = 1
for element in range(start, stop):
product = product * element
return product
x = multiply_list(1, 4)
10 Consider the following program:
def main():
num = eval(input("Enter a number: "))
for i in range(3):
num = num * 2
print(num)
main()
Suppose the input to this program is 2, what is the
output ?
Tahun kabisat adalah tahun yang bulan Pebruarinya berjumlah 29 hari. Berikut ini adalah ketentuan
2.1 untuk menentukan tahun kabisat atau tidak:
• Jika angka tahun itu habis dibagi 400, maka tahun itu sudah pasti tahun kabisat.
• Jika angka tahun itu tidak habis dibagi 400 tetapi habis dibagi 100, maka tahun itu sudah pasti
bukan merupakan tahun kabisat.
• Jika angka tahun itu tidak habis dibagi 400 dan tidak habis dibagi 100 akan tetapi habis dibagi 4,
maka tahun itu merupakan tahun kabisat.
• Jika angka tahun tidak habis dibagi 400, tidak habis dibagi 100, dan tidak habis dibagi 4, maka
tahun tersebut bukan merupakan tahun kabisat.
(Referensi: https://id.wikipedia.org/wiki/Tahun_kabisat)
Buatlah program Python untuk menentukan apakah suatu tahun merupakan tahun kabisat atau tidak.
Contoh tampilan:
Masukkan Tahun : XXXXX (input)
Tahun XXXX merupakan TAHUN KABISAT (output)