Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python Shorts Notes

Uploaded by

enet5566
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Shorts Notes

Uploaded by

enet5566
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Python Shorts Notes

What is Python?

Python is a versatile programming language that is:

• 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.

• High-Level Language: It abstracts complex details, allowing developers to focus on


programming rather than technicalities.

• 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.

Our First Program

Here's a simple Python program to display a message:

Python Character Set

Python supports various types of characters:

• Letters: A to Z, a to z

• Digits: 0 to 9

• Special Symbols: +, -, *, /, etc.

• Whitespaces: Includes spaces, tabs, newlines, etc.

• Other Characters: Supports all ASCII and Unicode characters.

Rules for Identifiers

• Start with a letter or underscore (_).

• Can contain letters, digits, and underscores.

• Case-sensitive (e.g., name and Name are different).


Data Types

Python has several built-in data types:

• Integers: Whole numbers (e.g., 5)

• Strings: Text (e.g., "Hello")

• Float: Decimal numbers (e.g., 3.14)

• Boolean: True or False values

Variables

Keywords

Keywords are reserved words in Python and cannot be used as identifiers. For example:

• False, True, None, if, else, etc.


Comments in Python

Types of Operators

Operators are symbols that perform operations on variables or values:

1. Arithmetic Operators: +, -, *, /, %, **

2. Relational Operators: == , !=, >, <, >=, <=

3. Assignment Operators: =, +=, -=, *=, /=, %=

4. Logical Operators: not, and, or

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 {}.

How f-strings work:

• You prefix the string with an f or F.

• 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.

Key Characteristics of a List:

• Ordered: Elements in a list maintain their order, so you can access them by their index.

• Mutable: You can modify a list (add, update, remove items).

• 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:-

A tuple is a collection of items in Python that is:

• Immutable: You cannot change, add, or remove items once it's created.

• Ordered: The items stay in the same order.

• Defined with Parentheses: You create a tuple using parentheses ( ).


Practice question:-

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.

1st method:- by using copy() method

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:

• Unordered: The order of items doesn't matter.

• Mutable: You can change, add, or remove items.

• No Duplicate Keys: Each key can only appear once.


NESTED DICTIONARIES IN PYTHON:-

What is a Nested Dictionary?

• 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.

Why Use Nested Dictionaries?

• 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

• Unordered: The elements in a set do not have a specific sequence.

• 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)

classrooms_needed = len(unique_subjects) # The number of unique 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.

1st method – Normal by using normal concept

2nd methods :-
Q4 . Figure out a way to store 9 & 9.0 as separate values in the set.

1st method :-

We have to store one value in the string format

2nd methods:-

We create a tuples pair for creation

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:-

You might also like