Docsity

Python full study notes, Study notes of Programming Languages

Partial preview of the text

Download Python full study notes and more Study notes Programming Languages in PDF only on Docsity!

Complete

Python for

Beginners

Notes by Rishabh Mishra

Rishabh Mishra

Save For Later

7+ Years in Data Analytics

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 01 Introduction to Python

  • What is programming
  • What is Python
  • Popular programming languages
  • Why Python
  • Career with Python What is Programming Language? Programming is the process of creating sets of instructions that tell a computer how to perform specific tasks. These instructions, known as code, are written in programming languages that computer understand and executes to carry out various operations, such as solving problems, analysing data, or controlling device. Popular programming languages: Python, C, C++, Java, Go, C#, etc. What is Python? Python is a high-level programming language known for its simplicity and readability. Just like we use Hindi language to communicate and express ourselves, Python is a language for computers to understand our instructions & perform tasks. Note: Python was created by Guido van Rossum in 1991.

Ques2: Write a program to print numbers from 1 to 10 In above examples we can see that Python is simple in writing & reading the code. Careers with Python Python is not only one of the most popular programming languages in the world, but it also offers great career opportunities. The demand for Python developers is growing every year. Python Tutorial Playlist: https://www.youtube.com/ @RishabhMishraOfficial

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 02 Python Installation & Setup + Visual Studio Code Installation

  • Python installation
  • Visual Studio Code installation Install Python Step 1: Go to website: https://www.python.org/downloads/ Step 2: Click on “Download Python” button (Download the latest version for Windows or macOS or Linux or other) Step 3: Run Executable Installer Step 4: Add Python to Path Step 5: Verify Python Was Installed on Windows Open the command prompt and run the following command: python --version Install Python IDE (code editor) Step 1: Go to website: https://code.visualstudio.com/download Step 2: Click on “Download” button (Download the latest version for Windows or mac or Linux or other) Step 3: Run Executable Installer Step 4: Click “Add to Path” Step 5: Finish & launch the app Popular Python IDE : VS Code, PyCharm, Jupyter Notebook & more Python Tutorial Playlist: Click Here https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg

Comments: Comments are used to annotate codes, and they are not

interpreted by Python. It starts with the hash character # Comments are used as notes or short descriptions along with the code to increase its readability.

Running the Python Code

  • Create a new text file and inside it write – print(“Welcome to the Python Course by Rishabh Mishra”)
  • Save file with extension .py – firstcode.py
  • Open command prompt on windows (or Terminal on MacOS)
  • Enter the location where firstcode.py file is saved – cd downloads
  • Finally run the file as – python firstcode.py

Python Execution Flow

Python Code Execution Steps

  1. Lexical Analysis: The interpreter breaks down the code into smaller parts called tokens, identifying words, numbers, symbols, and punctuation.
  2. Syntax Parsing: It checks the structure of the code to ensure it follows the rules of Python syntax. If there are any errors, like missing parentheses or incorrect indentation, it stops and shows a SyntaxError.
  3. Bytecode Generation: Once the code is validated, the interpreter translates it into a simpler set of instructions called bytecode. This bytecode is easier for the computer to understand and execute.
  4. Execution by PVM: The Python Virtual Machine (PVM) takes the bytecode and runs it step by step. It follows the instructions and performs calculations, assigns values to variables, and executes functions.
  5. Error Handling and Output: If there are any errors during execution, like trying to divide by zero or accessing a variable that doesn't exist, the interpreter raises an exception. If the code runs without errors, it displays any output, such as printed messages or returned values, to the user.

Python Syntax

The syntax of the Python programming language, is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system & by human readers). my_name = "Madhav" my_name = Madhav # Use quotes "" for strings in Python

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 04

Variables in Python

  • What is a Variable
  • Variables - examples
  • Variable Naming Rules

Variables in Python

A variable in Python is a symbolic name that is a reference or pointer to an object. In simple terms, variables are like containers that you can fill in with different types of data values. Once a variable is assigned a value, you can use that variable in place of the value. We assign value to a variable using the assignment operator (=). Syntax: variable_name = value Example: greeting = "Hello World" print(greeting)

Variable Examples

Python can be used as a powerful calculator for performing a wide range of arithmetic operations. PythonLevel = "Beginner" # pascal case pythonLevel = "Beginner" # camel case pythonlevel = "Beginner" # flat case python_level = "Beginner" # Snake case

x = 10 print(x+1) # add number to a variable a, b, c = 1, 2, 3 print(a, b, c) # assign multiple variables

Variable Naming Rules

  1. Must start with a letter or an underscore ( _ ).
  2. Can contain letters, numbers, and underscores.
  3. Case-sensitive (my_name and my_Name are different).
  4. Cannot be a reserved keyword (like for, if, while, etc.).

_my_name = "Madhav"

for = 26

# ‘for’ is a reserved word in Python Python Tutorial Playlist: Click Here https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg

Python Tutorial Playlist: Click Here https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 06

Type Casting in Python

  • What is type casting
  • Type Casting examples
  • Type Casting - Types

Type Casting

Type casting in Python refers to the process of converting a value from one data type to another. This can be useful in various situations, such as when you need to perform operations between different types or when you need to format data in a specific way. Also known as data type conversion. Python has several built-in functions for type casting: int(): Converts a value to an integer. float(): Converts a value to a floating-point number. str(): Converts a value to a string. list(), tuple(), set(), dict() and bool()

Type Casting Examples

Basic examples of type casting in python:

Converting String to Integer:

str_num = "26" int_num = int(str_num) print(int_num) # Output: 26 print(type(int_num)) # Output:

int_num = int(str_num) print(int_num) # Output: 26 print(type(int_num)) # Output:

Converting a value to boolean:

bool(0) # Output: False bool(1) # Output: True Python Tutorial Playlist: Click Here https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 07

Input Function in Python

  • Input Function – Definition
  • Input Function – Example
  • Handling Different Data Types

Input Function in Python

The input function is an essential feature in Python that allows to take input from the user. This is particularly useful when you want to create interactive programs where the user can provide data during execution. Also known as user input function. How input Function Works: The input function waits for the user to type something and then press Enter. It reads the input as a string and returns it.

Example:

Prompting the user for their name

name = input("Enter your name: ")

Displaying the user's input

print("Hello, " + name + "!")

Input Function – Add 2 Numbers

A simple program that takes two numbers as input from the user and prints their sum.

Prompting the user for the first and second number

num1 = input("Enter the first number: ") num2 = input("Enter the second number: ")

Python Tutorial Playlist: Click Here https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg

PYTHON TUTORIAL FOR BEGINNERS Source: www.youtube.com/@RishabhMishraOfficial Chapter - 08

Operators in Python

  • What are Operators
  • Types of Operators
  • Operators Examples

Operators in Python

Operators in Python are special symbols or keywords used to perform operations on operands (variables and values). Operators : These are the special symbols/keywords. Eg: + , * , /, etc. Operand : It is the value on which the operator is applied.

# Examples

Addition operator '+': a + b

Equal operator '==': a == b

and operator 'and': a > 10 and b < 20

Types of Operators

Python supports various types of operators, which can be broadly categorized as:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Identity Operators
  7. Membership Operators