Home » Python » Python programs

Create number variables (int, float and complex) and print their types and values in Python

Here, we are going to learn how to create number variables to store integer, float and complex numbers in Python, how to print their types and values?
Submitted by IncludeHelp, on April 26, 2019

The task is to create number variables to store integer, float and complex number, we have to print their types and values also in python.

Python numbers

There are three types of numbers in python:

  1. Integer number
  2. Float number
  3. Complex number

In the below-given program, we are creating different variables to store the numbers and printing their types and values.

Python code to create number variables, print types and values

# Python code to create number variables, 
# print types and values

# creating number variables and assigning values
a = 10      # integer
b = 10.23   # float 
c = 10+2j   # complex

# printing types
print("type(a): ", type(a))
print("type(b): ", type(b))
print("type(c): ", type(c))

# printing values
print("value of a: ", a)
print("value of b: ", b)
print("value of c: ", c)

Output

type(a):  <class 'int'>
type(b):  <class 'float'>
type(c):  <class 'complex'>
value of a:  10
value of b:  10.23
value of c:  (10+2j)

Assigning integer number in binary, decimal, octal, and hexadecimal format

# Assigning integer number in binary, decimal, 
# octal, and hexadecimal format

# creating integer variables and assigning values
# in different format
a = 123     # integer (decimal format)
b = 0o123   # integer (octal format)
c = 0x123AF # integer (hexadecimal format)
d = 0b11101 # integer binary format

# printing types
print("type(a): ", type(a))
print("type(b): ", type(b))
print("type(c): ", type(c))
print("type(d): ", type(d))

# printing values
print("value of a: ", a)
print("value of b: ", b)
print("value of c: ", c)
print("value of d: ", d)

Output

type(a):  <class 'int'>
type(b):  <class 'int'>
type(c):  <class 'int'>
type(d):  <class 'int'>
value of a:  123
value of b:  83
value of c:  74671
value of d:  29





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.