• 2-qism
  • 3-qism
  • Python 2 va Python 3 orasidagi farq




    Download 5.67 Mb.
    bet21/31
    Sana22.10.2022
    Hajmi5.67 Mb.
    #27828
    1   ...   17   18   19   20   21   22   23   24   ...   31
    Bog'liq
    Uzb coders qollanma
    TITUL 2 (3)

    Python 2 va Python 3 orasidagi farq


    Ushbu sahifadan Python 3.x (bu yerda 3.8.3) versiyasini yuklab oling va yuklangan faylni (bu yerda python-3.8.3.exe ) ishga tushiring. Pythonning ikkita bir-biri bilan mos tushmaydigan (incompatible) versiya bor - 2 va 3. Bularning asosiy farqlari quyidagicha:

    1. Print funksiyasi

    Python 2. Kod:
    print 'Python', python_version()
    print 'Hello, World!'
    print('Hello, World!')
    print "text", ; print 'print more text on the same line'
    Natija:
    Python 2.7.6
    Hello, World!
    Hello, World!
    text print more text on the same line
    Ko‘rib turganingizdek, Python 2 da print funksiyasidan ikkita usulda ham foydalanish mumkin !
    Python 3. Kod:
    print('Python', python_version())
    print('Hello, World!')


    print("some text,", end="")
    print(' print more text on the same line')
    Natijasi:
    Python 3.4.1
    Hello, World!
    some text, print more text on the same line
    Python 3 da print funksiyasini qavssiz ishlatish:
    print 'Hello, World!'
    Natijasi:
    File "", line 1
    print 'Hello, World!'
    ^
    SyntaxError: invalid syntax


    1. Butun sonlarni bo‘lish

    Python 2:
    print 'Python', python_version()
    print '3 / 2 =', 3 / 2
    print '3 // 2 =', 3 // 2
    print '3 / 2.0 =', 3 / 2.0
    print '3 // 2.0 =', 3 // 2.0
    Natija:
    Python 2.7.6
    3 / 2 = 1
    3 // 2 = 1
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0

    Python 3:
    print('Python', python_version())
    print('3 / 2 =', 3 / 2)
    print('3 // 2 =', 3 // 2)
    print('3 / 2.0 =', 3 / 2.0)
    print('3 // 2.0 =', 3 // 2.0)

    Natija:
    Python 3.4.1
    3 / 2 = 1.5
    3 // 2 = 1
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0


    1. Unicode

    Python 2 ASCII str() satr turiga, alohida unicode() turiga ega, lekin byte() turiga ega emas. Python 3 unicode(), byte() va bityarray() turlariga ega.


    1. Xrange()

    Python 2dan farqli o’laroq Python 3da xrange() funksiyasi yo’q, unda range() xrange() kabi ishlaydi.

    1. next() va .next()

    Python 2 da ikkalasidan next() funksiyasi va .next() metodidan foydalanish mumkin. Python 3 da faqat next() funksiyasi aniqlangan, .next() metodidan foydalanish xatoni yuzaga keltiradi.
    Python 2. Kod va natijasi:
    print 'Python', python_version()


    my_generator = (letter for letter in 'abcdefg')


    next(my_generator)
    my_generator.next()
    Python 2.7.6


    'b'

    Python 3. Kod va natijasi:


    print('Python', python_version())


    my_generator = (letter for letter in 'abcdefg')


    next(my_generator)
    Python 3.4.1


    'a'

    Ko‘proq ma’lumotni ushbu sahifadan olish mumkin: https://www.geeksforgeeks.org/important-differences-between-python-2-x-and-python-3-x-with-examples/


    2-qism


    1-savol
    Kompyuteringizdagi terminalda Python interaktiv tarjimonini (boshqacha nomi Python interaktiv rejimi) ishga tushiring. Keyin cake so‘zini kiritib ko‘ring:
    >>> cake
    Nima bo‘ldi?



    2-savol
    Endi yana urinib ko‘ring, lekin so‘zni qo‘shtirnoq ichiga qo‘ying:
    >>> "cake"
    Nima bo‘ldi?


    3-savol
    Endi ushbu ikki qatorni bajarishga urinib ko‘ring:
    >>> cake = "yummy"
    >>> cake
    Nima bo‘ldi?


    4-savol
    Endi ushbu qatorni bajarishga urinib ko‘ring:
    >>> "cake" = "yummy"
    Nima bo‘ldi?

    Izoh: literalga tayinlab bo‘lmaydi degan xato xabari chiqadi.

    5-savol
    Quyida kod berilgan:
    amy = turtle.Turtle()
    amy.color("blue")
    distance = 50
    amy.forward(distance)
    amy.right(90)
    Kodning ushbu qismlaridan qaysi biri o‘zgaruvchi nomlari (a.k.a. identifikatorlari) va literallar?

    Izoh: sonlar va satrlar literallar.


    3-qism


    1-savol
    Quyida "len" funksiyasi bilan terminalingizda sinab ko‘rishingiz mumkin bo‘lgan turli xil misollar bor.
    Siz ularni sinab ko‘rayotib, quyidagi savollarni yodda saqlang:
    Uzunlikni hisoblashda bo‘shliq (space)lar hisoblanadimi?
    O‘zgaruvchiga satr tayinlab, keyin o‘zgaruvchida "len" metodidan foydalana olasizmi?
    Butun sonning uzunligini olish mumkinmi?


    Download 5.67 Mb.
    1   ...   17   18   19   20   21   22   23   24   ...   31




    Download 5.67 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    Python 2 va Python 3 orasidagi farq

    Download 5.67 Mb.