Introduction
- Programming is giving instructions to a computer.
- Instructions are understood by computer to perform operations or task.
- We Have data and these instructions process data to derive results.
- Python 3.7 is the stable version as of now
- 3.9 is latest version
- High Level interpreted language
- Mostly used IDE is Jupyter Notebook or pycharm or vscode
- Each python file is a represented as a module
- Any folder that has empty __init__.py Is known as a package as per python vocabulary
- Files not in package can be accessed directly
- Python Data Structures
- Integers
- Floating Point
- Lists
- order stet of datatype
- is mutable
- Dictionary
- Array
- To check type of a variable
- type(b1)
- Complex Numbers
- Print formatting
- Lists
- Has homogeneous structure
- Example
- Numbers = ["One","Two","Three"]
- Iterate
- For n in numbers
- print(n)
- Methods
- append()
- pop()
- reverse()
- Nested Lists
- used for matrix
- List Comprehensive
- Tuples
- Sets
- does not allow duplicates
- Dictionaries
- Key Value Pairs
- Nested Dictionaries
- Creating Function
- def function name(args):
- To call body use function name
- Iterators and Generators
- Map
- pass list to function directly in one line
- Reduce Function
- Filter Function
- Lambda Function
- Range Function
- gets list of numbers in range
- range(0,6)
- Input
- name=input("what is your name")
- type(name)
- String
- len
- gives length of String
- S="Hello World"
- S[1:] will print entire string from first letter
- S.upper()->uppercase
- s.lower()->lowercase
- s.split()->split in list
- printformatting
- Python program has following components
- Objects
- Functions also called as methods
- Properties
- Class
- __init__()
- Opens up the class factory to initialise the class objects.
- Acts As a constructor of the class
- Initialise the class with default variables.
- This method is mandatory for all classes to have.
- sys Is a library which is included by default in Python.
- sys.version Gives us the current Python version.
- Using virtual environment’s
- A virtual environment is an environment for every component where it has its own version of python and required dependencies
- This is done to resolve python version dependency compatibility issues.
- Instead of getting dependences from the common installed, instance of python in operating system, which may Create issues If dependency is updated, every component uses Dependencies from Its own compatible virtual environment Python version.
- There is a standalone Python environment for every application/component.
- Third-party components sometimes provide version conflicts.
- To install dependences from a file In a virtual environment, use
- pip -r name_of_file
- This help’s to distribute our package with dependencies with packaged versions.
- Always work in a virtual environment.
- A New way To set up a virtual environment in python is using UV.
- Code organisation
- run.py
- This is a script that launch the program.
- This script may have names such as run.py, main.py etc
- Used to start application.
- Processing
- As the functional part of the code
- Utils
- Naming valuables in python.
- In Utility files which are generally part of util folder, we may have Empty files like __init__.py.
- We do this to differentiate them from source files or module files.
- Any folder, which has a blank __init__.py File is known as a package.
- This has to be an empty file.
- Src
- Contains the source files
- Controller
- In the web application, it may contain controller.
- PEP8 and Zen of python
- Pep8 Is the style guidelines for python code.
- Always use four spaces/never tabs
- Use meaningful names for classes and objects.
- Use formatters.
- All this is done by our IDE.
- In the Python command prompt fire command
- import this
- This populates the Zen of Python.
- Advantages of python
- Portable
- Write once and use anywhere
- Readable
- Productive
- Standard libraries
- Many functionalities are already written for data, science, machine, learning, and AI.
- Flexible
- Multi use That is, we can use it in Web app, data, manipulation, data, science, CSV generation, et cetera.
- Datatypes/objects in python and their manipulation
- Everything is object in python
- Every object has a unique ID Called as identity
- Every object has a unique type
- Each object will have a value
- Mutable and immutable
- Mutable Data can be changed. Immutable data cannot be changed.
- Identity helps you to identify if the Viable or object is actually changeable or not. This should not be referred by value.
- id(value) Can be used to print the ID of Data type for example id(2) ,id(12) etc.
- Numbers by default are immutable.
- When we assign a new value, it creates a new memory space and variable point to that space.
- So never check mutability from value.
- Only reference changes value is never changed.
- Set is mutable In python, it’s ID remain same even after adding values.
- Numbers, Booleans and operations and operations on them
- Numbers are also called as integers.
- Booleans are predictable That is, they can either be true or false.
- Real numbers are floating point numbers with decimals.
- There are also Complex numbers, we have one part is real and other is fictional That is iota value’s etc.
- Each one of them has a name.
- If during division, we put a “//“ Then the whole number is given as response and decimal values are ignore.
- ** will give power of first number to second number
- 2**3 =8
- Boolean is denoted as 0 or 1, where zero means false. And one means true.
- Booleans are automatically upcasted during operations to 0 or 1.
- Using bool function We can downcast boolean values.
- Any positive value in boolean Represents a true.
- We can also perform logical operations on Boolean values.
- AND, OR, NOT operations
- Check their truth Tables for resultant values.
- sys.float_info gives us the float information about our system.
- Fraction class from fractions package helps us to deal with fractions.
- Decimal class from decimal package helps us to deal with decimal values.
- String – index, slice and encoding
- String provides text objects that can perform operations on their values like indexing, slicing, et cetera.
- Strings are immutable.
- Strings are index zero till length of string -1.
- We can encode the strengths, which have special characters, for example, strings with Japanese language, et cetera.
- we can encode and decode using encode and decode functions
- References
Comments
Post a Comment