Python Data Types:
Type Casting
Two types of typecasting:
- Implicit i.e. int→float
- Explicit i.e. str→int
Variable Scope:
Scope Hierarchy in Python:
- Built-In Scope: Contains built-in names and is always available.
- Global Scope: Names defined at the top level of a script or module.
- Enclosing Scope: Names in the local scopes of enclosing functions (non-local but also not global).
- Local Scope: Names defined within a function.
Primitive vs Non-Primitive Data types
- Primitive Data Types: int, float, bool, str
- Non-Primitive Data Types: list, tuple, dict, set, frozenset
Data Structure in Python
Python has the following built-in data structures: List, dictionary, tuple and set. These are all considered non-primitive data structures, meaning they are classed as objects.
Mutability and Immutability
Mutability refers to data inside the data structure that can be modified. For example, you can either change, update, or delete the data when needed. A list is an example of a mutable data structure. The opposite of mutable is immutable. An immutable data structure will not allow modification once the data has been set. The tuple is an example of an immutable data structure.
Three way of iterating a Dictionary
args
vs kwargs
with args
we can pass any number of non-Keyword variable
with kwargs
we can pass any number of Keyword arguments
Types of Error in Python
- Syntax Error
- Exception Error: Exception Error occur in the runtime.
File Handling
There are four modes of handling file in Python:
- r - open for reading file
- r+ - open for reading and writing
- w - open for writing
- a - open for editing and appending
with open closes the file automatically
Procedural Programming Paradigm
Advantages of Procedural Programming: Disadvantage of Procedural Programming:
Traditional VS Pure Functions
Functional Programming Paradigm
In python function is first class citizen. That means they are on the same level of string and numbers. Advantages of Functional Programming:
Benefits of Pure Functions
map and filter()
map(compare_function, list) ⇒ None ⇒ None ⇒ X ⇒ Y ⇒ None
filter(compare_function, list) ⇒ X ⇒ Y
Comprehensions
Comprehensions in Python are a way to create a new sequence from an already existing sequence. There are four main types of comprehensions in Python:
- List comprehension
- Dictionary comprehension
- Set comprehension
- Generator comprehension
List comprehension
The syntax for list comprehension is:
Dictionary comprehension
The syntax for dictionary comprehension is:
Set comprehension
The syntax for Set comprehension is:
Generator comprehension
Generator comprehensions are also very similar to lists with the variation of using curved brackets instead of square brackets. They are also more memory efficient as compared to list comprehensions.
The syntax for Generator comprehension is:
Order in Which Module is Searched
- Current Directory Path
- Built in Module directory
- PYTHONPATH
- Installation dependent default directory