4-Amaliy: Pythonda operatorlar bilan ishlash.
Boshqa dasturlash tillarida bo'lgani kabi, raqamlar bilan qo'shish, ayirish, ko'paytirish va bo'lish operatorlaridan ham foydalanish mumkin.
number = 1 + 2 * 3 / 4.0
print(number)
test_object("number")
test_function("print")
success_msg("To'g'ri javob!")
Mavjud bo'lgan boshqa operator modul (%) operatori bo'lib, bo'linishning butun sonini qaytaradi. dividend % bo'luvchi = qoldiq.
remainder = 11 % 3
print(remainder)
test_object("remainder")
test_function("print")
success_msg("To'g'ri javob!")
Ikkita ko'paytirish belgilaridan foydalanish kuch munosabatlarini yaratadi.
squared = 7 ** 2
cubed = 2 ** 3
print(squared)
print(cubed)
test_object("squared")
test_object("cubed")
test_function("print")
success_msg("To'g'ri javob!")
Python qo'shish operatori yordamida satrlarni birlashtirishni qo'llab-quvvatlaydi:
helloworld = "hello" + " " + "world"
print(helloworld)
test_object("helloworld")
test_function("print")
success_msg("To'g'ri javob!")
Python, shuningdek, takroriy ketma-ketlik bilan satr hosil qilish uchun satrlarni ko'paytirishni qo'llab-quvvatlaydi:
lotsofhellos = "hello" * 10
print(lotsofhellos)
test_object("lotsofhellos")
test_function("print")
success_msg("To'g'ri javob!")
Ro'yxatlarni qo'shish operatorlari bilan birlashtirish mumkin:
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
test_object("even_numbers")
test_object("odd_numbers")
test_object("all_numbers")
test_function("print")
success_msg("To'g'ri javob!")
Xuddi satrlarda bo'lgani kabi, Python ko'paytirish operatori yordamida takrorlanadigan ketma-ketlik bilan yangi ro'yxatlarni shakllantirishni qo'llab-quvvatlaydi:
print([1,2,3] * 3)
test_function("print")
success_msg("To'g'ri javob!")
Ushbu mashqning maqsadi mos ravishda x va y o'zgaruvchilarning 10 ta nusxasini o'z ichiga olgan x_list va y_list deb nomlangan ikkita ro'yxat yaratishdir. Bundan tashqari, siz yaratgan ikkita ro'yxatni birlashtirib, x va y o'zgaruvchilari har biri 10 mart
x = object()
y = object()
# TODO: change this code
x_list = [x]
y_list = [y]
big_list = []
print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
x = object()
y = object()
# TODO: change this code
x_list = [x] * 10
y_list = [y] * 10
big_list = x_list + y_list
print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
test_object("x_list")
test_object("y_list")
test_object("big_list")
test_function("print")
success_msg("To'g'ri javob!")