Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Shorts Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

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

1.wap to print leap year


2. WAP of Armstrong number

3.WAP to print palindrome


OR

4.Swap two variable


5.wap to print prime number

6.WAP a program factorial of a number


7. Generate Random Numbers
8. Print Fibonacci Series
8. Square Root of a number
9. Swap two elements in a list

You might also like