Variables can store data of different types, each data type has different characteristics and can do different things. Everything is an object in Python programming, Python data types are classes and variables are instances (objects) of these classes
Python has the following data types built-in by default, in these categories:
str
int
, float
, complex
list
, tuple
, range
dict
set
, frozenset
bool
bytes
, bytearray
, `memoryview
Here is very brief summary of important data typesInt
, or integer, is a whole number, positive or negative, without decimalsfloat
, or "floating point number" is a number, positive or negative, containing one or more decimals.
complex
numbers are written with a "j" as the imaginary part:x = 1 # int
y = 2.8 # float
z = 1j # complex
Casting
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
The string is a sequence of characters. Python supports Unicode characters. Generally, strings are represented by either single or double-quotes.
name = "techtrekking"
name = 'techtrekking'
Python list is collection of values. Main feature of python list is that, it can hold different types of datatypes in same list
id_list = [ 11, 55, 'arya', 5.5 ]
Tuple is very simillar to list however it is immutable.
Another differentiation is that list is represented by square brackets []
and tuple by Parentheses ()
id_tuple = [ 11, 55, 'arya', 5.5 ]
Python Dictionary is an unordered sequence of data of key-value pair.
team_data = {'first_name': 'arya', 'last_name' : 'stark', 'episode':'10'}
boolean type stores either True
or False
value
a = True
b = False
print(type(a)) # Output: <class 'bool'>
print(type(b)) # Output: <class 'bool'
print(9==8)
print(None==None)
Python will consider datatype based on value of variable however many times we need specific datatype and this can be explicitely assigned as below
x = str("Hello World")
x = int(20)
x = float(20.5)
x = complex(1j)
x = list(("apple", "banana", "cherry"))
x = tuple(("apple", "banana", "cherry"))
x = range(6)
x = dict(name="John", age=36)
x = set(("apple", "banana", "cherry"))
x = frozenset(("apple", "banana", "cherry"))
x = bool(5)
x = bytes(5)
x = bytearray(5)
x = memoryview(bytes(5))
post by Pravin