Python căn bản (6): Python Numbers

Bài trước: Python căn bản (5): Data Types

-----

[Từ điển]

6. Python Numbers

6.1 Numeric types in Python

There are three numeric types in Python: int, float and complex

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

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

Example:

x = 1    # int

y = 2.8  # float

z = 1j   # complex

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.

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.

Example

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.

Example

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.

Example

x = 3+5j

y = 5j

z = -5j


print(type(x))

print(type(y))

print(type(z))

6.2 Type Conversion

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

Example

x = 1    # int

y = 2.8  # float

z = 1j   # complex


#convert from int to float:

a = float(x)


#convert from float to int:

b = int(y)


#convert from int to 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.

6.3 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.

Example

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

import random


print(random.randrange(1,10))

6.4 Python Casting

Specify a Variable Type

There may be times when you want to specify a type on a variable. This can be done with casting. Python is an object-oriented language, and as such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)

float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)

str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals

Example

Integers:

x = int(1)   # x will be 1

y = int(2.8) # y will be 2

z = int("3") # z will be 3


print(x)

print(y)

print(z)

Floats:

x = float(1)     # x will be 1.0

y = float(2.8)   # y will be 2.8

z = float("3")   # z will be 3.0

w = float("4.2") # w will be 4.2


print(x)

print(y)

print(z)

print(w)

Strings:

x = str("s1") # x will be 's1'

y = str(2)    # y will be '2'

z = str(3.0)  # z will be '3.0'


print(x)

print(y)

print(z)

6.5 Exercise

1. Which is NOT a legal numeric data type in Python?

A. int

B. long

C. float

D. complex

2. What will be the result of the following code:

print(int(35.88))

A. 35

B. 35.00

C. 36

D. 35.88

3. What will be the result of the following code:

print(float(35))

A. 35

B. 35.0

C. ‘35’

D. 0.35

4. Write a Python program that allows user input two numbers, then calculate the product and sum of two numbers.

Input:

Number 1:3

Number 2:4

Expected output:

Product: 12

Sum: 7

5. Write a Python program that allows user input the length and width of the rectangle, then calculate the perimeter and area of the rectangle.

Input:

Length:2.5

Width 3.0

Expected output:

Perimeter: 11.0

Area: 7.5

----- 

Cập nhật: 27/9/2024

Bài sau: Python căn bản (7): Python Strings

-----

[Nội dung tham khảo từ w3schools, pynative và Internet]

Bạn muốn học Python căn bản tại Đà Lạt, liên hệ