Python Numbers

Python Numbers – Python дахь Тоонууд #

There are three numeric types in Python:

Python-д гурван төрлийн тоон утга байдаг

  • int – Бүхэл тоо
  • float – Бутархай тоо
  • complex – Нийлмэл тоо

Variables of numeric types are created when you assign a value to them:

Хувьсагчид тоон төрлийн утга оноох үед үүсгэнэ:

Example – Жишээ #

x = 1    # int
y = 2.8  # float
z = 1j   # complex
 

To verify the type of any object in Python, use the type() function:

Python дээр аливаа зүйлсыг шалгахын тулд type() функцийг ашиглаарай:

Example – Жишээ #

print(type(x))
print(type(y))
print(type(z))
 

Int – Бүхэл тоо #

Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.

Int, буюу integer нь бүхэл тоо бөгөөд эерэг эсвэл сөрөг, хязгааргүй урттай.

Example – Жишээ #

x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))

Float – Бутархай тоо #

Float, or “floating point number” is a number, positive or negative, containing one or more decimals.

Float буюу “floating point number” нь нэг эсвэл хэд хэдэн аравтын бутархай агуулсан эерэг эсвэл сөрөг тоо.

Example – Жишээ #

Floats:

Бутархай тоонууд:

x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

Float can also be scientific numbers with an “e” to indicate the power of 10.

Float нь мөн “e” гэсэн тэмдэглэгээтэйгээр 10-ын зэргийн тэмдэглэгээтэйгээр бичигдэж болно.

Example – Жишээ #

Floats:

Бутархай тоонууд:

x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))

Complex – Нийлмэл тоо #

Complex numbers are written with a “j” as the imaginary part:

Complex тоо нь “j” гэсэн тэмдэглэгээтэйгээр дүрслэгддэг.

Example – Жишээ #

Complex:

Нийлмэл тоо:

x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))

Type Conversion – Төрөл Хувиргах #

You can convert from one type to another with the int()float(), and complex() methods:

int(), float(), болон complex() аргуудыг ашиглан нэг төрлөөс нөгөө рүү хөрвүүлж болно:

Example – Жишээ #

Convert from one type to another:

Нэг төрлөөс нөгөө төрөл рүү хүрвүүлэх:

x = 1     # int
y = 2.8   # float
z = 1j    # complex
# int-ээс float руу хөрвүүлэх:
a = float(x)
# float-ээс int руу хөрвүүлэх:
b = int(y)
# int-ээс complex руу хөрвүүлэх:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

Note: You cannot convert complex numbers into another number type.

Тэмдэглэл: Complex тоог өөр төрлийн тоонд хөрвүүлж болохгүй.

Random Number – Санамсаргүй тоо #

Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers:

Python нь random() функцгүй боловч, Python нь санамсаргүй тоо үүсгэх random гэсэн суурилуулсан модулийг агуулдаг.

Example – Жишээ #

Import the random module, and display a random number between 1 and 9:

random модуль(солигч эд анги) оруулж, 1-ээс 9 хүртлэх тоог санамсаргүй байдлаар харуулна:

import random
print(random.randrange(1, 10))

Powered by BetterDocs

Leave a Reply