2-Amaliy: Pythonda o’zgaruvchilar bilan ishlash.
Butun sonni aniqlash uchun quyidagi sintaksisdan foydalaning:
myint = 7
print(myint)
test_object("myint")
test_function("print")
success_msg("To'g'ri javob!")
Suzuvchi nuqta raqamini aniqlash uchun siz quyidagi belgilardan birini ishlatishingiz mumkin:
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
test_object("myfloat")
test_function("print")
success_msg("To'g'ri javob!")
Satrlar bitta tirnoq yoki qo'sh tirnoq bilan aniqlanadi.
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
test_object("mystring ")
test_function("print")
success_msg("To'g'ri javob!")
Ularning orasidagi farq shundaki, qo'sh tirnoqlardan foydalanish apostroflarni kiritishni osonlashtiradi (holbuki ular bitta tirnoq ishlatilsa, satrni tugatadi)
mystring = "Don't worry about apostrophes"
print(mystring)
test_object("mystring")
test_function("print")
success_msg("To'g'ri javob!")
Oddiy operatorlar raqamlar va satrlarda bajarilishi mumkin:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
test_object("one")
test_object("two")
test_object("three")
test_object("hello")
test_object("world")
test_object("helloworld")
test_function("print")
success_msg("To'g'ri javob!")
Bu kabi bir qatorda bir nechta o'zgaruvchilarga "bir vaqtning o'zida" topshiriqlar bajarilishi mumkin
a, b = 3, 4
print(a, b)
test_object("a")
test_object("b")
test_function("print")
success_msg("To'g'ri javob!")
Raqamlar va satrlar o'rtasida operatorlarni aralashtirish qo'llab-quvvatlanmaydi:
# This will not work!
one = 1
two = 2
hello = "hello"
print(one + two + hello)
test_object("one")
test_object("two")
test_object("hello")
test_function("print")
success_msg("To'g'ri javob!")
Ushbu mashqning maqsadi satr, butun son va suzuvchi nuqta raqamini yaratishdir. Satr mystring deb nomlanishi va unda “salom” so‘zi bo‘lishi kerak. Suzuvchi nuqta raqami myfloat deb nomlanishi va 10.0 raqamini, butun son esa myint deb nomlanishi va 20 raqa
# change this code
mystring = None
myfloat = None
myint = None
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
# change this code
mystring = "hello"
myfloat = 10.0
myint = 20
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
test_object("mystring")
test_object("myfloat")
test_object("myint")
success_msg("To'g'ri javob!")