- Python is a dynamically-typed language, which means you don't need to explicitly declare the data type of a variable. Here are some of the most commonly used data types in Python:
Built-in Data Types¶
Type | Name |
---|---|
Text Type | str |
Numeric Types | int, float, complex |
Sequence Types | list, tuple, range |
Mapping Type | dict |
Set Types | set, frozenset |
Boolean Type | bool |
Binary Types | bytes, bytearray, memoryview |
None Type | NoneType |
Text Type :
- String
In [ ]:
a = "Hello World" #str
print(type(a))
<class 'str'>
Numeric Types:
- int
- float
- Complex
In [ ]:
b = 30 #int
print(type(b))
c = 2.5 #float
print(type(c))
d = 2+1j #complex
print(type(d))
<class 'int'> <class 'float'> <class 'complex'>
Sequence Types
- List
- Tuple
- range
In [ ]:
e = ["train", "bus", "bike"] #list
print(type(e))
f = ("train", "bus", "bike") #tuple
print(type(f))
g = range(5) #range
print(type(g))
<class 'list'> <class 'tuple'> <class 'range'>
Mapping Type
- dict
In [ ]:
h = {"name" : "Raj", "age" : 30} #dict
print(type(h))
<class 'dict'>
Set Types
- set
- frozenset
In [ ]:
i = {"train", "bus", "bike"} #set
print(type(i))
j = frozenset({"train", "bus", "bike"}) #frozenset
print(type(j))
<class 'set'> <class 'frozenset'>
Boolean Type
- bool
In [ ]:
k = True #bool
print(type(k))
<class 'bool'>
Binary Types
- bytes
- bytearray
- memoryview
In [ ]:
l = b"Hi" #bytes
print(type(l))
m = bytearray(10) #bytearray
print(type(m))
n = memoryview(bytes(10)) #memoryview
print(type(n))
<class 'bytes'> <class 'bytearray'> <class 'memoryview'>
None Type
- NoneType
In [ ]:
o = None #NoneType
print(type(o))
<class 'NoneType'>
Type Casting in Python:¶
- There may be times when you want to specify a type on to a variable. This can be done with type casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.
- You can convert one data type to another using constructors or type casting functions like int(), float(), str(), list(), etc. Here are some examples:
Text Type :
- String
In [ ]:
a = str("Hello World") #str
print(a)
print(type(a))
Hello World <class 'str'>
Numeric Types:
- int
- float
- Complex
In [ ]:
b = int(25) #int
print(b)
print(type(b))
c = float(2.5) #float
print(c)
print(type(c))
d = complex(2+1j) #complex
print(d)
print(type(d))
25 <class 'int'> 2.5 <class 'float'> (2+1j) <class 'complex'>
Sequence Types
- List
- Tuple
- range
In [ ]:
e = list(("train", "bus", "bike")) #list
print(e)
print(type(e))
f = tuple(("train", "bus", "bike")) #tuple
print(f)
print(type(f))
g = range(5) #range
print(g)
print(type(g))
['train', 'bus', 'bike'] <class 'list'> ('train', 'bus', 'bike') <class 'tuple'> range(0, 5) <class 'range'>
Mapping Type
- dict
In [ ]:
h = dict(name="Raj", age=30) #dict
print(h)
print(type(h))
{'name': 'Raj', 'age': 30} <class 'dict'>
Set Types
- set
- frozenset
In [ ]:
i = set(("train", "bus", "bike")) #set
print(i)
print(type(i))
j = frozenset(("train", "bus", "bike")) #frozenset
print(j)
print(type(j))
{'bike', 'train', 'bus'} <class 'set'> frozenset({'bike', 'train', 'bus'}) <class 'frozenset'>
Boolean Type
- bool
In [ ]:
k = bool(5) #bool
print(k)
print(type(k))
True <class 'bool'>
Binary Types
- bytes
- bytearray
- memoryview
In [ ]:
l = bytes(10) #bytes
print(l)
print(type(l))
m = bytearray(10) #bytearray
print(m)
print(type(m))
n = memoryview(bytes(10)) #memoryview
print(n)
print(type(n))
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' <class 'bytes'> bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') <class 'bytearray'> <memory at 0x7f5709e10940> <class 'memoryview'>
Scope in Python¶
- In Python, the term "scope" refers to the region of a program where a particular identifier (e.g., a variable or a function) is accessible.
- Python has several levels of scope, including global scope, local scope, and nested scope. Let's explore these scopes in more detail:
Local Scope:¶
- Variables defined within a function have local scope.
- Local variables are only accessible within the function in which they are defined.
- Variables with the same name can exist in different functions without causing conflicts because they are in different local scopes.
In [6]:
def local_scope_example():
local_variable = 5
print(local_variable)
local_scope_example()
#print(local_variable) # This would result in an error because local_variable is not defined in the global scope.
5
- Function Inside Function : As explained in the example above, the variable x is not available outside the function, but it is available for any function inside the function
In [7]:
def myfunc():
x = 500
print(x)
myfunc()
500
In [8]:
# Function Inside Function
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()
300
Global Scope:¶
- Variables defined outside of any function or class have global scope. Global variables can be accessed from anywhere in the program, including within functions and classes.
- To modify a global variable's value from within a function, you need to use the global keyword.
In [1]:
global_variable = 10
def modify_global():
global global_variable
global_variable += 5
modify_global()
print(global_variable) # Output: 15
15
- Naming Variables : If you operate with the same variable name inside and outside of a function, Python will treat them as two separate variables, one available in the global scope (outside the function) and one available in the local scope (inside the function)
In [2]:
x = 500
def myfunc():
print(x)
myfunc()
print(x)
500 500
In [3]:
x = 500
def myfunc():
x = 600
print(x)
myfunc()
print(x)
600 500