Unit I - PYTHON
Unit I - PYTHON
Used for:
1) Web Development: Frameworks like Django, Flask.
2) Data Science and Analysis: Libraries like Pandas, NumPy, Matplotlib.
3) Machine Learning and AI: TensorFlow, PyTorch, Scikit-learn.
4) Automation and Scripting: Automate repetitive tasks.
5) Game Development: Libraries like Pygame.
6) Web Scraping: Tools like BeautifulSoup, Scrapy.
7) Desktop Applications: GUI frameworks like Tkinter, PyQt.
8) Scientific Computing: SciPy, SymPy.
9) Internet of Things (IoT): MicroPython, Raspberry Pi.
10) DevOps and Cloud: Automation scripts and APIs.
11) Cybersecurity: Penetration testing and ethical hacking tools.
Why PYTHON
1) Easy to Learn and Use: Python’s simple and readable syntax makes it beginner-friendly.
2) Cross-Platform Compatibility: Python runs seamlessly on Windows, macOS, and Linux.
3) Extensive Libraries: Includes robust libraries for tasks like web development, data
analysis, and machine learning.
4) Dynamic Typing: Variable types are determined automatically at runtime, simplifying code
writing.
5) Versatile: Supports multiple programming paradigms, including object-oriented, functional,
and procedural programming.
6) Open Source: Python is free to use, distribute, and modify.
SIMPLE PROGRAMS:
print("Hey there!")
------------------------------------------------------------------------------------------------------------------------
print(5 * 10)
------------------------------------------------------------------------------------------------------------------------
name = input("What's your name? ")
print("Nice to meet you, " + name)
------------------------------------------------------------------------------------------------------------------------
# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of Males: ", x)
print("Number of Females: ", y)
------------------------------------------------------------------------------------------------------------------------
# taking three inputs at a time
x, y, z = input("Enter Total number of students, Male Students & Female Students: ").split()
print("Total number of students: ", x)
print("Number of Male students: ", y)
print("Number of Female students: ", z)
------------------------------------------------------------------------------------------------------------------------
# Prompting the user for input
1 YASH TRIPATHI
age_input = input("Enter your age: ")
------------------------------------------------------------------------------------------------------------------------
# Converting the input to an integer
age = int(age_input)
if guess == secret:
print("Correct! You win!")
else:
print("Nope, it was", secret)
Note: Python doesn’t automatically load every tool — because that would be super slow. So you
only load what you need, and in this case, random gives you all kinds of cool, unpredictable
behavior.
------------------------------------------------------------------------------------------------------------------------
import random
num = random.randint(1, 10)
print(num)
------------------------------------------------------------------------------------------------------------------------
import random
colors = ["red", "blue", "green", "yellow"]
pick = random.choice(colors)
print("The chosen color is:", pick)
------------------------------------------------------------------------------------------------------------------------
import random
def roll_dice():
return random.randint(1, 6)
print("You rolled:", roll_dice())
------------------------------------------------------------------------------------------------------------------------
2 YASH TRIPATHI
import random
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
print("You rolled:", die1, "and", die2)
print("Total:", die1 + die2)
------------------------------------------------------------------------------------------------------------------------
We can use ‘%’ operator. % values are replaced with zero or more value of elements. The
formatting using % is similar to that of ‘printf’ in the C programming language.
%d –integer
%f – float
%s – string
%x –hexadecimal
%o – octal
------------------------------------------------------------------------------------------------------------------------
# Taking input from the user
num = int(input("Enter a value: "))
add = num + 5
# Output
print("The sum is %d" %add)
------------------------------------------------------------------------------------------------------------------------
input() raw_input()
raw_input() function takes the input from the
1 input() function take the user input. user.
Its syntax is -: Its syntax is -:
2 input(prompt) raw_input(input)
3 It takes only one parameter that is prompt. It takes only one parameter that is the input.
4 It return the input that it takes. Its return type is of string.
It converts the input into a string by removing the
5 trailing newline It is only introduced in python 2.0 version
6 blocks until input received hang ’till user inputs
7 “hello world” “hello world” but string
8 foo in snake_case
3 YASH TRIPATHI
OPERATORS in general are applied to operate upon values and variables. They are typical
symbols employed for logical and arithmetic operations. In this article, we shall examine various
types of operators in Python.
4 YASH TRIPATHI
a = 13
b = 33
print(a > b) False
print(a < b) true
print(a == b) false
print(a != b) true
print(a >= b) false
print(a <= b) true
------------------------------------------------------------------------------------------------------------------------
Logical Operators in Python
Python Logical operators perform Logical AND, Logical OR and Logical NOT operations. It is
used to combine conditional statements.
The precedence of Logical Operators in Python is as follows:
Logical not
logical and
logical or.
a = True
b = False
print(a and b) false
print(a or b) true
print(not a false
------------------------------------------------------------------------------------------------------------------------
Bitwise Operators in Python
Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on
binary numbers.
Bitwise Operators in Python are as follows:
Bitwise NOT
Bitwise Shift
Bitwise AND
Bitwise XOR
Bitwise OR
a = 10
b=4
print(a & b) 0
print(a | b) 14
print(~a) -11
print(a ^ b) 14
print(a >> 2) 2
print(a << 2) 40
------------------------------------------------------------------------------------------------------------------------
Assignment Operators in Python
Python Assignment operators are used to assign values to the variables. This operator is used to
assign the value of the right side of the expression to the left side operand.
Example of Assignment Operators in Python:
a = 10
5 YASH TRIPATHI
b=a
print(b) 10
b += a
print(b) 20
b -= a
print(b) 10
b *= a
print(b) 100
b <<= a
print(b) 102400
------------------------------------------------------------------------------------------------------------------------
Identity Operators in Python
In Python, is and is not are the identity operators both are used to check if two values are located on
the same part of the memory. Two variables that are equal do not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Example of Identity Operators in Python:
------------------------------------------------------------------------------------------------------------------------
a = 10
b = 20
c=a
print(a is not b) True
print(a is c) true
------------------------------------------------------------------------------------------------------------------------
PYTHON DATA types are the categorization or classification of data items. It is the type of value
which indicates what operations can be done on a given data. As everything is an object in Python
programming, Python data types are classes and variables are objects (instances) of these classes.
The following are the built-in or standard data types in Python:
ASSIGNMENT
6 YASH TRIPATHI