5-Amaliy: Pythonda tarmoqlanuvchi jarayonlarni dasturlash.
Python shartlarni baholash uchun mantiqiy mantiqdan foydalanadi. Ifoda solishtirilganda yoki baholanganda True va False mantiqiy qiymatlari qaytariladi. Masalan:
x = 2
print(x == 2) # prints out True
print(x == 3) # prints out False
print(x < 3) # prints out True
success_msg("To'g'ri javob!")
"Va" va "yoki" mantiqiy operatorlar murakkab mantiqiy ifodalarni yaratishga imkon beradi, masalan:
name = "John"
age = 23
if name == "John" and age == 23:
print("Your name is John, and you are also 23 years old.")
if name == "John" or name == "Rick":
print("Your name is either John or Rick.")
success_msg("To'g'ri javob!")
The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list:
name = "John"
if name in ["John", "Rick"]:
print("Your name is either John or Rick.")
success_msg("To'g'ri javob!")
Kod bloklari yordamida Pythonning "if" iborasidan foydalanishga misol:
statement = False
another_statement = True
if statement is True:
# do something
pass
elif another_statement is True: # else if
# do something else
pass
else:
# do another thing
pass
success_msg("To'g'ri javob!")
Double equals operatori "=="dan farqli o'laroq, "is" operatori o'zgaruvchilarning qiymatlariga emas, balki misollarning o'ziga mos keladi. Masalan:
x = [1,2,3]
y = [1,2,3]
print(x == y) # Prints out True
print(x is y) # Prints out False
success_msg("To'g'ri javob!")
Mantiqiy ifodadan oldin "not" dan foydalanish uni o'zgartiradi:
print(not False) # Prints out True
print((not False) == (False)) # Prints out False
success_msg("To'g'ri javob!")
Mantiqiy ifodadan oldin "not" dan foydalanish uni o'zgartiradi:
# change this code
number = 10
second_number = 10
first_array = []
second_array = [1,2,3]
if number > 15:
print("1")
if first_array:
print("2")
if len(second_array) == 2:
print("3")
if len(first_array) + len(second_array) == 5:
print("4")
if first_array and first_array[0] == 1:
print("5")
if not second_number:
print("6")
# change this code
number = 16
second_number = 0
first_array = [1,2,3]
second_array = [1,2]
if number > 15:
print("1")
if first_array:
print("2")
if len(second_array) == 2:
print("3")
if len(first_array) + len(second_array) == 5:
print("4")
if first_array and first_array[0] == 1:
print("5")
if not second_number:
print("6")
test_object("number")
test_object("second_number")
test_object("first_array")
test_object("second_array")
success_msg("To'g'ri javob!")