0% found this document useful (0 votes)
13 views7 pages

Python Manual Data TypesV2

Every value in Python has a data type. The main data types are numbers (int, float, complex), Boolean, and strings. Numbers represent numeric values and have subclasses of int, float, and complex. Booleans represent true or false values. Strings are sequences of characters that can be indexed. Data types can be checked using the type() function. Values can be converted between types using functions like int(), float(), and str(). Python uses print() to output values and input() to get user input. Various operators can be used to perform operations on values of different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views7 pages

Python Manual Data TypesV2

Every value in Python has a data type. The main data types are numbers (int, float, complex), Boolean, and strings. Numbers represent numeric values and have subclasses of int, float, and complex. Booleans represent true or false values. Strings are sequences of characters that can be indexed. Data types can be checked using the type() function. Values can be converted between types using functions like int(), float(), and str(). Python uses print() to output values and input() to get user input. Various operators can be used to perform operations on values of different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Data Types

Every value in Python has a datatype. Since everything is an object in Python programming, data
types are actually classes and variables are instance (object) of these classes.

Numbers
• Integers, floating point numbers and complex numbers falls under Python numbers
category. They are defined as int, float and complex class in Python.

• We can use the type() function to know which class a variable or a value belongs to.

Example 1:
a=5 #data type is implicitly set to integer
print(a, " is of type", type(a))
Output :
5 is of type <class 'int'>

Examples 2:
a = 2.5 #data type is changed to float
print(type(a))
Output :
<class 'float'>

Example 3:
a = 1 + 2j #data type is changed to complex number
print(type(a))
Output :
<class 'complex'>

Boolean
Boolean represents the truth values False and True

Examples 1:
a = True #a is a boolean type
print(type(a))
Output :
<class 'bool'>

Python Strings
 String is sequence of Unicode characters.
 We can use single quotes or double quotes to represent strings.
 Multi-line strings can be denoted using triple quotes, ''' or """.
 A string in Python consists of a series or sequence of characters - letters, numbers, and
special characters.
 Strings can be indexed - often synonymously called subscripted as well.
 Similar to C, the first character of a string has the index 0.

Examples 1:
s = “Trident Academy"
print(s)
Output :
Trident Academy

Conversion between Datatypes


• We can convert between different data types by using different type conversion functions
like int(), float(), str() etc.

Examples 1:
float(5) #convert interger to float using float() method
Output :
5.0

Examples 2:
int(100.5) #convert float to integer using int() method
Output :
100

Examples 3:
str(20) #convert integer to string
Output :
'20‘

Python Input and Output


Python Output
• We use the print() function to output data to the standard output device
Examples 1:
print("Hello World")
Output :
Hello World

Examples 2:
a = 10
print("The value of a is", a)
Output :
The value of a is 10

print() function separates its outputted arguments with spaces. This behavior can be
changed, too. The keyword argument that can do this is named sep (like separator).
Look at the example →

Example 1:

Output

Example 2:

Output

Python Input
Examples 3:
#we can use keyword arguments to format the string
print("Hello {name}, {greeting}".format(name="students", greeting="Good Morning"))
Output :
Hello students, Good Morning

Operators
Python Arithmetic Operators
Python Identity Operators
Python Membership Operators

You might also like