Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
Reserved words like:if,else,for,True,False, None,etc.
Save file as -- ".py"
Run in terminal:
---python filename.py
Python IDLE
Built-in Data Types
Numeric Types:
Sequence Types:
Mapping Type:
Set Types:
Boolean Type:
Binary Types:
User-defined Data Types
Created using classes, arrays, and modules.
Use type() function to check the type of a variable.
Types of Operators
Arithmetic Operators:
Assignment Operators:
Comparison Operators:
Logical Operators:
Bitwise Operators:
Identity Operators:
Membership Operators:
Methods :
1. Input Method in Python
2. Checking return type of input() method in python
3. Checking return type of input() method in python
4. Converting string type to int() in Python
5. Converting string type to float() in Python
6. Taking int value at run time in Python
7. Taking int value and print their sum in Python
Types Of Control Flow Statement
In this type of execution flow, the statements are executed one after the other sequentially. By using sequential statements, we can develop simple programs.sequential statements are executed line by line.
Sequential statements (demo1.py)
Conditional: Statements are executed based on the condition. As shown above in the flow graph, if the condition is true then one set of statements are executed, and if false then the other set. Conditional statements are used much in complex programs.
There are three types of conditional statements in python. They are as follows:
The if statement contains an expression/condition. As per the syntax colon (:) is mandatory otherwise it throws a syntax error. The condition gives the result as a bool type, either True or False. If the condition result is True, then if block statements will be executed. If the condition result is False, then if block statements wonβt execute.
The if statement contains an expression/condition.The condition gives the result as bool type, which means either True or False. If the condition result is True, then if block statements will be executed. If the condition result is False, then else block statements will be executed.
The if statement contains an expression/condition.The condition gives the result as bool type, which means either True or False. The conditions are evaluated in the order written. If one condition becomes True then the set of statements associated with that particular condition are executed and the execution comes out without checking other conditions.
If we want to execute a group of statements multiple times, then we should go for a looping kind of execution. There are two types of loops available in python.
For loops are typically used to iterate over a sequence (such as a list, tuple, string, or range) or other iterable objects. The loop executes once for each item in the sequence.
While loops repeatedly execute a block of code as long as a condition is true.
Loops can be nested inside other loops. The inner loop executes completely for each iteration of the outer loop.
Example :Python for and while loops can have an else clause. The else block executes when the loop finishes normally (i.e., not terminated by a break statement).
Example :A string is a sequence of characters enclosed in single ('), double ("), or triple (''' or """) quotes.Triple quotes are used for multi-line strings.
1. Creating and Printing Strings
2. Multi-line Strings
3. Quotes Inside Strings
4. Empty String
5. Accessing Characters (Indexing)
6. Loop Through String
7. Slicing Strings
8. Immutability of Strings
9. String Concatenation and Repetition
10. Length of String
11. Membership Operators
12. Comparing Strings
13. Removing Spaces
14. Finding Substrings
15. Counting Substrings
16. Replacing Strings
17. Splitting Strings
18. Joining Strings
19. Changing Case
20. Formatting Strings
21. Character Data Type
A function is one that contains a group of statements or a block of code to perform a certain task. The advantages of using functions are:
Example : print() is a predefined function in python that prints output on the console.
Types of functions in python:
There are many categories based on which we can categorize the functions. This categorization is based on who created it.
The functions which come installed along with python software are called predefined or built-in functions. We have covered some inbuilt functions in examples of earlier chapters. Some of them are stroid(), type(), input(), print() etc.
The functions which are defined by the developer as per the requirement are called user-defined functions. In this chapter, we shall concentrate on these kinds of functions that are user-defined.
From creating a function to using it, these are the two things that are done.
In the function-related terminologies above, it was clearly explained what is what and how are they used. All the points together define the function. To summarize the definition contains β def keyword, name for the function, parentheses, parameters(optional), colon (:), body, return(optional).
Syntax to define a function in Python :-def display(): print("welcome to function")
After defining a function, we need to call to execute the function. While calling the function, we have to call with the same name of the function which we used while defining it, otherwise we will get an error.
def display(): print("welcome to function") display() display() display()
Based on the parameters, functions can be categorized into two types. They are:
A function that has no parameters in the function definition is called a function without parameters. The syntax is given below.
def name_of_the_function(); //body of the function to perform operations
The return statement is included in the function body, and it returns some results after doing the operations. Some point about the return statement
Syntax ;-
def name_of_function(par1,par2,..); //body of function to perform operations return result
Output :-
sum of a and b: 15
subtraction of a and b: 5
Syntax :-
In python a module means a saved python file. This file can contain a group of classes, methods, functions and variables. Every file with .py or .python extension is called a python file, in turn a module.
If we want to use other members(variable, function, etc) of a module in your program, then you should import that module by using the import keyword. After importing you can access members by using the name of that module.
This imports the entire module, making its functions, classes, and variables accessible using the module name as a namespace. For example:
Importing Specific Items :-
You can import specific functions, classes, or variables from a module using the from keyword:
This imports only the specified items, allowing you to use them directly without the module name.
For example:Importing with an Alias:
You can use the as keyword to give an imported module or item a different name:
This can be useful for shortening long module names or avoiding name conflicts.
For example:Importing All Items:
You can import all items from a module using the * wildcard:
In Python, a list is a versatile and commonly used data structure that allows you to store a collection of items. Lists are ordered, mutable, allow duplicates, and can store heterogeneous data types. Lists are part of the core Python data structures and provide a wide range of operations to manipulate stored elements effectively.
[]
and elements are separated by commas. We can create a list in python by using square brackets β[ ]'. The elements in the list should be comma-separated.
Example: Creating an empty list
Example: Creating a list with elements
l = list(range(0, 10))
names = ["Mohan", 10, True]
names[0]
or names[-1]
list[start:stop:step]
. Example: n[2:5:2]
for
or while
loops.List items can be changed after creation, which makes them mutable. This is one of the key differences between lists and tuples.
y = x
x[:]
or x.copy()
Aliasing causes any change in one reference to reflect in the other. Cloning avoids this by creating a new object.
a + b
a * 2
These operators allow you to build and modify lists in flexible ways.
[1,2,3] < [2,2,3]
compares element-wise until difference is found.20 in x
or 90 not in x
returns True/False.Membership checks are useful for conditionals, filtering, and validation tasks.
Lists within lists. Useful for representing matrices or structured data.
Create lists in one line using expressions. It provides a concise way to create and transform lists.
With condition:
List comprehensions are not only cleaner but also more efficient in performance than traditional loops in many cases.