Numeric data types in Python

What are numeric data types in Python? How do you use numeric data types in Python? Lets find out together with TipsMake.com!

What are numeric data types in Python? How do you use numeric data types in Python? Let's find out together with TipsMake.com!

 

Python has three numeric data types:

  • int
  • float
  • complex

Numeric variables are created when you assign a value to them:

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

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

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

Int

Int, or integer, is a number, positive or negative, that is not a decimal and has unlimited length.

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

Float

A float, or 'floating point number,' is a positive or negative number containing one or more decimal places.

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

Float can also be a scientific number, with 'e' representing a power of 10.

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

Complex

Complex numbers are written with j as the imaginary part:

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 using the methods int(), float(), and complex().

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 to other number types.

Random number

Python doesn't have a built-in function random()to generate random numbers, but it does have a built-in module called `random` that you can use to generate random numbers:

For example:

Input a random modulo, and display a random number between 1 and 9:

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

Above are the things you need to know about numbers in Python . Hopefully, this Python lesson is useful to you.

Related posts
Other Python articles
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup