Python Shorts Notes
Python Shorts Notes
What is Python?
• Simple & Easy: Python has a clear and readable syntax, making it beginner-friendly.
• Free & Open Source: Anyone can use Python for free, and its source code is available for
modification.
• Developed by Guido van Rossum: Python was created in the late 1980s to be easy to learn
and fun to use.
• Portable: Python code can run on different operating systems (like Windows, macOS, and
Linux) without changes.
• Letters: A to Z, a to z
• Digits: 0 to 9
Variables
Keywords
Keywords are reserved words in Python and cannot be used as identifiers. For example:
Types of Operators
1. Arithmetic Operators: +, -, *, /, %, **
Type Conversion
In Python, you can mix data types, but it may lead to errors:
Type Casting
Input in Python
STRINGS
Slicing :- Slicing in Python allows you to extract a part of a string (or other sequences like lists) by
specifying a starting and ending position.
Practice Question
The f in f"..." refers to f-strings, which are a feature in Python used to format strings. F-strings allow
you to embed expressions inside string literals, using curly braces {}.
• Inside the string, you can place variables or expressions inside {}.
List in Python:
In Python, a list is a built-in data type that is used to store an ordered collection of items. Lists are
mutable, meaning you can change, add, or remove elements after the list is created. Lists can contain
elements of different data types, such as integers, strings, floats, or even other lists.
• Ordered: Elements in a list maintain their order, so you can access them by their index.
• Allows duplicates: Lists can contain multiple instances of the same value.
• Can store different data types: A list can hold items of different types (integers, strings, floats,
etc.).
List Slicing
List slicing is a technique to extract a portion or a sublist from a list. You can access a range of
elements from a list by specifying a starting and ending index. The syntax is similar to string slicing.
Valid:-
Not a valid implementation:-
Tuple in python:-
• Immutable: You cannot change, add, or remove items once it's created.
Q1 WAP to ask user to enter names of their 3 favourite movies & store them in a list .
1st method:-
2nd method:-
Q2 :- WAP to check if a list contains a palindrome of element.
2nd method:-
Q3 :- WAP to count the number of students with the “A” grade in the following tuple.
[“C”,”D”,”A”,”A”,”B”,”B”,”A”]
Store the above values in a list & sort them from “A” to “D”.
DICTIONARIES IN PYTHON
Definition: A dictionary is a way to store data in key-value pairs. Each key is unique and is used to
access its corresponding value.
Properties:
• A nested dictionary is a dictionary that contains another dictionary (or dictionaries) as its
value.
• This allows you to organize data in a hierarchical way, making it easier to store complex data
structures.
• They are useful for representing data that has multiple levels of information. For example,
you might want to store information about students, their subjects, and the scores they
received in those subjects.
Dictionary Method:-
1. keys()
• What it does: This method gives you a list of all the keys in the dictionary. Keys are the
unique identifiers used to access values.
2. items()
• What it does: This method returns all the key-value pairs in the dictionary as a list of tuples.
Each tuple contains a key and its corresponding value.
3. update(newDict)
• What it does: This method allows you to add new key-value pairs to the dictionary or update
existing ones using another dictionary (newDict).
4. values()
What it does: This method returns a list of all the values in the dictionary. Values are the data
associated with the k
5. get(key)
• What it does: This method retrieves the value associated with a specified key. If the key
doesn’t exist, it returns None or a default value if specified.
SET IN PYTHONS
What is a Set?
• A set is a collection of items that is unordered and contains only unique elements.
• This means that in a set, you cannot have duplicate items, and the order of items does not
matter.
Properties of Sets
• Unique Elements: Each element must be unique. If you try to add a duplicate, it will be
ignored
Set Methods:-
PRACTICE QUESTIONS
2. You are a given list of subjects for students , Assume one classroom is required for 1 subjects . How
many classrooms are needed by all students.
"python", "java", "C++", "python", "javascript", "java", "python", "java", "C++", "C"
subjects = ["python", "java", "C++", "python", "javascript", "java", "python", "java", "C++", "C"]
unique_subjects = set(subjects)
Q3 . WAP to enter marks of 3 subjects from the user and store them in a dictionary . Start with an
empty dictinary & add one by one . Use subject nme as key & marks as value.
2nd methods :-
Q4 . Figure out a way to store 9 & 9.0 as separate values in the set.
1st method :-
2nd methods:-
LOOPS in Python :-
What are Loops?
Loops are used in programming to repeat instructions multiple times. They allow you to execute a
block of code as many times as you need without having to write the same code repeatedly.
Practice Questions :-
1. Break Statement
The break statement is used to exit a loop immediately when a certain condition is met.
This means that once the break statement is executed, the loop stops running, and the
program continues with the next line of code outside the loop.
2. Continue Statement
The continue statement is used to skip the current iteration of the loop and move on to
the next iteration. This means that when continue is executed, the rest of the code in the
loop for that particular iteration is skipped.
Practice:
Functions :-
Recursion
Recursion is a technique where a function calls itself to solve smaller instances of the same
problem. Each recursive function has a base case to stop the recursion.
Output :- 15
Summary
• Functions help organize code into reusable blocks.
• Default parameters provide default values for function parameters.
• Recursion allows functions to call themselves with smaller problems until
reaching a base case.
File I/O in Python
Python allows you to interact with files on your computer. You can read data
from files and write data to them. Files can be of various types:
1. Text Files: These include files like .txt, .docx, and .log.
2. Binary Files: These include files like .mp4, .mov, .png, and .jpeg.
Practice Exercise:-