Notes
Notes
PYTHON: Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.
It is used for:
software development,
mathematics,
system scripting.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software development.
Execution Mode:
Batch Mode.
Interactive Mode :
Interactive mode is a command line shell. If we write a python program in the command line shell.
Typically the interactive mode is used to test the features of the python, or to run a smaller script that
may not be reusable.
Script Mode :
script mode is mainly used to develop business applications. In batch mode, we can write a group of
python statements in any one of the following editors or IDEs
Python Keywords:
Keywords in Python are reserved words that have special meanings and serve specific purposes in the
language syntax. Python keywords cannot be used as the names of variables, functions, and classes or
any other identifier.
Identifiers: Python Keywords are some predefined and reserved words in Python that have special
meanings. Keywords are used to define the syntax of the coding. The keyword cannot be used as an
identifier, function, or variable name. All the keywords in Python are written in lowercase except True
and False.
All the keywords in Python should be in lowercase except True and False.
Variables: In Python, variables are used to store data that can be referenced and manipulated during
program execution. A variable is essentially a name that is assigned to a value. Unlike many other
programming languages, Python variables do not require explicit declaration of type. The type of the
variable is inferred based on the value assigned.
Example:
x=5
name = "Samantha"
print(x)
print(name)
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Python Numbers
int
float
complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
Sequences in Python
Sequences are containers with items stored in a deterministic ordering. Each sequence data type comes
with its unique capabilities.
There are many types of sequences in Python. Let’s learn them with Examples.
1. Strings
2. Lists
3. Tuples
4. Bytes Sequences
5. Bytes Arrays
6. range() objects
Strings in Python
In python, the string is a sequence of Unicode characters written inside a single or double-quote. Python
does not have any char type as in other languages (C, C++), therefore, a single character inside the
quotes will be of type str only.
Lists in Python
Lists are a single storage unit to store multiple data items together. It’s a mutable data structure, therefore,
once declared, it can still be altered.
Tuples in Python
Just like Lists, Tuples can store multiple data items of different data types. The only difference is that
they are immutable and are stored inside the parenthesis ().
Mapping: The map() function is used to apply a given function to every item of an iterable, such as
a list or tuple, and returns a map object (which is an iterator).
Let's start with a simple example of using map() to convert a list of strings into a list of integers.
res = map(int, s)
print(list(res))
Python Operators
In the example below, we use the + operator to add together two values:
Example
print(10 + 5)
Arithmetic operators are used with numeric values to perform common mathematical operations:
Python Comparison Operators
Operator Precedence
Example
Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated
first:
print((6 + 3) - (6 + 3))
Multiplication * has higher precedence than addition +, and therefore multiplications are evaluated
before additions:I
Understanding input and output operations is fundamental to Python programming. With the print()
function, we can display output in various formats, while the input() function enables interaction with
users by gathering input during program execution.
Python input() function is used to take user input. By default, it returns the user input in form of a string.
Example:
Output
Logical errors are subtle bugs in the program that allow the code to run, but produce incorrect or
unintended results. These are often harder to detect since the program doesn’t crash, but the output is
not as expected.
Python Functions is a block of statements that does a specific task. The idea is to put some commonly
or repeatedly done task together and make a function so that instead of writing the same code again and
again for different inputs, we can do the function calls to reuse code contained in it over and over again.
Code Reuse
If….Else statement: In Python, If-Else is a fundamental conditional statement used for decision-making in
programming. If...Else statement allows to execution of specific blocks of code depending on the condition is True or
False.
if Statement
if statement is the most simple decision-making statement. If the condition evaluates to True, the block of code
inside the if statement is executed.
Example of If Statement:
i = 10
if (i > 15):
if....else Statement
if...else statement is a control statement that helps in decision-making based on specific conditions. When the if
condition is False. If the condition in the if statement is not true, the else block will be executed.
i = 20
print("i is positive")
else:
print("i is 0 or Negative")
Python For Loops are used for iterating over a sequence like lists, tuples, strings, and ranges.
For loop allows you to apply the same operation to every item within loop.
Using For Loop avoid the need of manually managing the index.
For loop can iterate over any iterable object, such as dictionary, list or any custom iterators.
Range function:
The Python range() function returns a sequence of numbers, in a given range. The most common use of it is to
iterate sequences on a sequence of numbers using Python loops.
Example
for i in range(5):
print()
In Python programming language there are two types of loops which are for loop and while loop. Using these
loops we can create nested loops in Python. Nested loops mean loops inside a loop. For example, while loop
inside the for loop, for loop inside the for loop, etc.