0% found this document useful (0 votes)
6 views11 pages

Python

The document provides a comprehensive overview of Python programming, covering various operators, control statements, built-in and user-defined functions, data structures like lists, tuples, and dictionaries, as well as data handling with CSV and database connections. It also includes examples of visualizations using charts and basic GUI components like radio buttons. Each section is structured with definitions, examples, and operations relevant to Python programming.

Uploaded by

sangulasai3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Python

The document provides a comprehensive overview of Python programming, covering various operators, control statements, built-in and user-defined functions, data structures like lists, tuples, and dictionaries, as well as data handling with CSV and database connections. It also includes examples of visualizations using charts and basic GUI components like radio buttons. Each section is structured with definitions, examples, and operations relevant to Python programming.

Uploaded by

sangulasai3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SECTION – A (5 × 7 = 35 Marks)

1. Various Operators in Python


Operators are symbols used to perform operations on variables and values in Python.

1. Arithmetic Operators

Used for mathematical operations.

Operator Description Example


+ Addition a+b
- Subtraction a - b
* Multiplication a * b
/ Division a/b
% Modulus a%b
** Exponent a ** b
// Floor division a // b

Example:

a = 10
b = 3
print(a + b)
print(a % b)

2. Relational (Comparison) Operators

Operator Example
== a == b
!= a != b
> a>b
< a<b
>= a >= b
<= a <= b

3. Logical Operators

Operator Meaning
and True if both conditions true
or True if any condition true
not Reverses result

Example:
x = 5
print(x > 2 and x < 10)

4. Assignment Operators

Operator Example
= x = 10
+= x += 5
-= x -= 5
*= x *= 2

5. Bitwise Operators

Operator Example
& a&b
| a|b
^ a^b
~ ~a

6. Membership Operators

Operator Example
in 'a' in word
not in 'b' not in word

7. Identity Operators

Operator Example
is a is b
is not a is not b

2. Control Statements in Python


Control statements control the flow of program execution.

1. Conditional Statements

if statement

x = 10
if x > 5:
print("Greater")
if-else

x = 5
if x % 2 == 0:
print("Even")
else:
print("Odd")

if-elif-else

marks = 80
if marks >= 90:
print("A")
elif marks >= 70:
print("B")
else:
print("C")

2. Looping Statements

for loop

for i in range(5):
print(i)

while loop

i = 1
while i <= 5:
print(i)
i += 1

3. Jump Statements

 break
 continue
 pass

Example:

for i in range(10):
if i == 5:
break

3. Built-in Functions and User Defined


Functions
Built-in Functions
These are predefined functions available in Python.

Examples:

Function Purpose
print() Display output
len() Length of object
type() Data type
sum() Adds numbers
max() Largest value

Example:

print(len("Python"))

User Defined Functions

Functions created by the programmer.

Example:

def add(a, b):


return a + b

print(add(5,3))

Advantages:

 Code reuse
 Easy debugging
 Better program organization

4. Main Components of a Function in


Python
A function has several components.

1. Function Definition

Defined using def keyword

def greet():

2. Parameters
Inputs passed to function.

def add(a, b):

3. Function Body

Statements executed in function.

def add(a,b):
result = a + b

4. Return Statement

Returns value to caller.

return result

5. Function Call

Calling the function.

add(5,3)

5. List and List Operations


A List is a collection of ordered and changeable elements.

Example:

numbers = [10,20,30,40]

List Operations

1. Accessing Elements

print(numbers[0])

2. Adding Elements

[Link](50)

3. Removing Elements

[Link](20)

4. Updating Elements

numbers[1] = 25
5. Length

print(len(numbers))

6. Tuple and Tuple Operations


A Tuple is an ordered immutable collection.

Example:

t = (10,20,30)

Tuple Operations

Access

print(t[1])

Concatenation

t1 = (1,2)
t2 = (3,4)
print(t1 + t2)

Repetition

print(t * 2)

Length

print(len(t))

7. Array and Operations on Arrays


An Array is a collection of elements of the same data type.

Example using Python array module:

import array
arr = [Link]('i',[1,2,3,4])

Array Operations

Traversal

for i in arr:
print(i)
Insertion

[Link](5)

Deletion

[Link](2)

Searching

print([Link](3))

8. Importing and Exporting Data between


CSV and DataFrames
Using pandas.

Import CSV to DataFrame


import pandas as pd

data = pd.read_csv("[Link]")
print(data)

Export DataFrame to CSV


data.to_csv("[Link]")

Advantages:

 Easy data handling


 Used in data analysis
 Supports large datasets

9. Procedure for Connecting with a


Database
Python connects to databases using connectors like [Link].

Steps

1. Import library

import [Link]
2. Create connection

conn = [Link](
host="localhost",
user="root",
password="1234",
database="testdb"
)

3. Create cursor

cursor = [Link]()

4. Execute query

[Link]("SELECT * FROM students")

5. Fetch data

for row in cursor:


print(row)

10. Charts
i. Scatter Chart
A Scatter Chart displays relationship between two variables.

Example:

import [Link] as plt

x=[1,2,3,4]
y=[10,20,25,30]

[Link](x,y)
[Link]()

Uses:

 Detect trends
 Show correlation

ii. Histogram
A Histogram represents frequency distribution of data.

Example:
import [Link] as plt

data=[10,20,20,30,30,30,40]
[Link](data)
[Link]()

Uses:

 Shows data distribution


 Identifies patterns

SECTION – B (5 × 3 = 15 Marks)
11. break Statement
The break statement terminates a loop immediately.

Example:

for i in range(10):
if i == 5:
break
print(i)

Output:

0 1 2 3 4

12. Comments in Python


Single Line Comment
# This is a comment

Multi-line Comment
"""
This is a
multi-line comment
"""

13. Data Types in Python


Common data types:
Type Example
int 10
float 10.5
str "Hello"
list [1,2,3]
tuple (1,2,3)
set {1,2,3}
dict {"a":1}

14. Dictionary Operations


Dictionary stores key-value pairs.

Example:

student = {"name":"Ram","age":20}

Operations:

print(student["name"]) # Access
student["age"] = 21 # Update
student["course"] = "CS" # Add
del student["age"] # Delete

15. Concatenating Tuples


Two tuples can be joined using + operator.

Example:

t1 = (1,2,3)
t2 = (4,5)

t3 = t1 + t2
print(t3)

Output:

(1,2,3,4,5)

16. Set in Python


A set is an unordered collection of unique elements.

Example:

s = {1,2,3,4}
Operations:

[Link](5)
[Link](2)
print(len(s))

17. Indexing and Slicing


Indexing

Accessing element by position.

name = "Python"
print(name[0])

Output:

Slicing

Accessing range of elements.

print(name[1:4])

Output:

yth

18. Radio Button


A Radio Button allows selecting only one option from multiple choices.

Example using Tkinter:

from tkinter import *

root = Tk()

v = IntVar()

Radiobutton(root,text="Male",variable=v,value=1).pack()
Radiobutton(root,text="Female",variable=v,value=2).pack()

[Link]()

You might also like