Q:

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

belongs to collection: Python basic programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Create integer variable by assigning binary value ... >>
<< Determine the type of an object in Python...