0% found this document useful (0 votes)
5 views6 pages

VTU_Python_Programming_Notes

The document provides an overview of various Python programming concepts including flow control statements, built-in functions, module importing, exception handling, and user-defined functions. It also covers list operations, dictionary methods, and statistical calculations like mean and standard deviation. Additionally, it includes examples of string manipulation, local vs global scope, and character frequency counting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

VTU_Python_Programming_Notes

The document provides an overview of various Python programming concepts including flow control statements, built-in functions, module importing, exception handling, and user-defined functions. It also covers list operations, dictionary methods, and statistical calculations like mean and standard deviation. Additionally, it includes examples of string manipulation, local vs global scope, and character frequency counting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Flow Control Statements in Python


Python includes:
- if, elif, else: decision making
Example:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

- for loop: iterates over a sequence


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

- while loop: repeats while condition is true


count = 0
while count < 3:
print(count)
count += 1

- break, continue, pass for loop control.

2. Built-in Functions
print(): prints output
input(): takes user input
len(): returns length
str(): converts to string
int(): converts to integer
float(): converts to float

Examples:
print("Hello")
x = input("Enter name: ")
print(len("Python"))
print(str(123))
print(int("10"))
print(float("3.14"))

3. Importing Modules
import math
print(math.sqrt(16))

from math import pi


print(pi)

4. Exceptions and Handling


try-except block:
try:
x = 10 / 0
except ZeroDivisionError:
print("Error!")

try-except-finally:
try:
x = int("abc")
except ValueError:
print("Invalid input")
finally:
print("Runs always")

5. Factorial and Binomial Coefficient


Factorial:
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
print(factorial(5))

Binomial:
def fact(n):
return 1 if n == 0 else n * fact(n - 1)
def binomial(n, k):
return fact(n) // (fact(k) * fact(n - k))
print(binomial(5, 2))

6. Output of Code Snippets


i.
eggs = 'bacon'
print("Before:", eggs)
update_global_variable()
print("After:", eggs)
# Output: bacon -> spam

ii.
eggs = 'spam'
print("Before:", eggs)
function_bacon()
print("After:", eggs)
# Output: spam -> spam

iii. Same as ii

8. String Concatenation and Replication


Concatenation:
s1 = "Hello"
s2 = "World"
print(s1 + " " + s2)

Replication:
print("Hi" * 3)

9. User Defined Functions


def greet(name):
print("Hello", name)
greet("Alice")

10. elif, for, while, break, continue


elif:
x = 20
if x < 10:
print("Low")
elif x < 30:
print("Medium")

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

while:
x=0
while x < 3:
print(x)
x += 1

break/continue in loops

11. Local vs Global Scope


x = 10 # Global
def my_func():
x = 5 # Local
print(x)
my_func()
print(x)
Using global:
def change():
global x
x = 20

12. List Operations


my_list = [4, 2, 9]
my_list.append(5)
my_list.remove(2)
print(9 in my_list)
my_list.sort()
my_list.reverse()

13. List Slicing


S = ['cat', 'bat', 'rat', 'elephant', 'ant']
S[1:5] -> ['bat', 'rat', 'elephant', 'ant']
S[:5] -> ['cat', 'bat', 'rat', 'elephant', 'ant']
S[3:-1] -> ['elephant']
S[:] -> full list

14. Dictionary in Python


dict = {'name': 'Alice', 'age': 25}
for key in dict:
print(key)

List vs Dictionary:
- List uses index, dict uses keys.

15. Program for Mean, Variance and Standard Deviation


import statistics
data = [10, 20, 30]
mean = statistics.mean(data)
variance = statistics.variance(data)
std_dev = statistics.stdev(data)

print("Mean:", mean)
print("Variance:", variance)
print("Standard Deviation:", std_dev)

16. Dictionary Methods: keys(), values(), items()


my_dict = {'a': 1, 'b': 2}
print(my_dict.keys()) # dict_keys(['a', 'b'])
print(my_dict.values()) # dict_values([1, 2])
print(my_dict.items()) # dict_items([('a', 1), ('b', 2)])

17. Dictionary with 10 Key-Value Pairs


d = {i: chr(65 + i) for i in range(10)}
print(d.keys())

18. Frequency of Characters using pprint


import pprint

message = "hello world"


count = {}
for char in message:
count.setdefault(char, 0)
count[char] += 1

pprint.pprint(count)

You might also like