Python??
Python??
◾ Python is an interpreted language similar to how 1. Web Applications An IDE (or Integrated Development Environment) is a
java operates with bytecode Web application framework libraries like django, program dedicated to software development. IDEs
◾ Once Python starts, you will see the interpreter Pyramid, Flask etc. make it very easy to develop and contains several tools like:
startup message, indicating version and platform. deploy simple as well as complex web applications. An editor designed to handle code (with, for
◾ You will also be given the python interpreter These frameworks are used extensively by various IT example, syntax highlighting and auto-completion)
prompt, i.e., ">>>" which is also known as python companies. Build, execution, and debugging tools
chevron prompt. 2. Game Development The different IDE"e for python are IDLE, Jupyter
◾ The ">>>"indicates that Python interpreter is Python is also used in the development of Notebook etc.
waiting for an expression or command. interactive games. PyGame which provides IDLE
◾ The interactive environment where we are functionality and a library for game development. Every Python installation comes with an Integrated
interacting with the Python interpreter is called the 3. Data Science and Data Visualization Development and Learning Environment, which
console or command shell Huge amount of data is being generated today by you'll see shortened to IDLE or even IDE. These are a
Advantages of Python web applications, mobile applications and other class of applications that help you write code more
◾ Most programs in Python require considerably devices. Companies need business insights from this efficiently. While there are many IDEs for you to
less number of lines of code to perform the same data. Python libraries like NumPy, Pandas and choose from, Python IDLE is very bare-bones, which
task compared to other languages like C. Matplotlib are extensively used in the process of makes it the perfect tool for a beginning
◾ So less programming errors and reduces the data analysis, including the collection, processing programmer.
development time needed also. and cleansing of data Python IDLE comes included in Python installations
◾ Simple and Easy to learn on Windows and Mac. If you're a Linux user, then you
◾ It's Opensource and Free
Variables
should be able to find and download Python IDLE
◾ Python is famous for being the "batteries are
Python has no command for declaring a variable.
Variables are created when you assign a value to it.
using your package manager. Once you've installed
included" language. it, you can then use Python IDLE as an interactive
◾ There are over 300 standard library modules
Data type of variable is determined based on data
given by user. So Python is known as dynamically
interpreter or as a file editor.
which contain modules and classes for a wide
typed language.
variety of programming tasks.
Boolean Operators for Flow Control if elif else Multiway Decision Statement Loops in python
Decision making statements available in python are Here, a user can decide among multiple options. A loop statement allows us to execute a statement or
• if statement The if statements are executed from the top down. group of statements multiple times as long as the
• if..else statements As soon as one of the conditions controlling the if is condition is true. Repeated execution of a set of
• nested if statements true, the statement associated with that if is statements with the help of loops is called iteration.
• if-elif ladder executed, and the rest of the ladder is bypassed. Loops statements are used when we need to run same
if statement If none of the conditions is true, then the final else code again and again, each time with a different value.
The simplest selection control statement is if statement will be executed. In Python Iteration (Loops) statements are of three
statement Syntax: if condition: Nested if types:
statements When you have an if statement(or if else or if elif 1. While Loop 2. For Loop 3. Nested Loops
Here, the condition after evaluation will be either else) inside another ifstatement(or if else or if elif [Link] Loop
true or false. else), it is called nesting The while loop in Python is used to iterate over a
if statement accepts boolean values - if the value is if (condition1): block of code as long asthe test expression
true then it will execute the block of statements statement (condition) is true.
below it otherwise not. if (condition2): We generally use this loop when we don't know the
if-else statement statement number of times toiterate beforehand.
If-else is called alternative execution # if Block is end here Syntax: while test_expression:
It provides two possibilities. The condition determines # if Block is end here Body of while
which possibility isexecuted. Built-in Functions Nested loop
If the condition is TRUE, the first block is executed but The python interpreter has several functions that Using loop inside another loop is known as nested
if condition isFALSE, another block of code is are always present for use. These functions are loop.
executed if (condition): known as Built-in Functions. Example #nested for loopSyntax:
statement(s) # Executes this block if There are some built-in functions in the Python n=eval(input("Enter number :"))
# condition is true programming language that can convert one type of for i in range(1,n):
else: data into another type. print("Multiplication Table of ",i)
statement(s)# Executes this block if For example, the int function can take any number for j in range(1,11):
# condition is false value and convert it into an integer. print(j,"*",i,"=",j*i)
for loop
Loop Control Statements Advantage of Functions in Python
The for loop is used to iterate over a sequence
Loop control statements change execution from its There are the following advantages of Python
It goes through the elements in any ordered
normal sequence. They are used to control the order functions.
sequence list, i.e., string, lists, tuples, the keys of
dictionary and other iterables.
of execution of the program based on the values and ◾ Using functions, we can avoid rewriting the same
logic. Sometimes we wish to terminate the current logic/code again and again in a program.
In each iteration step, a loop variable is set to a
value. It does not require the loop variable to set
iteration or even the whole loop without checking ◾ We can call Python functions multiple times in a
test expression. Python provides 3 types of Control program and anywhere in a program.
beforehand Syntax >>>for x in y :
Block 1
Statements: ◾ We can track a large Python program easily when
1. break it is divided into multiple functions.
else:
# Optional
2. continue ◾ Reusability is the main achievement of Python
3. pass functions.
Block 2 # excuted only when
the loop exits normally break and continue statements Mathematical functions
Range()
The break and continue statements are often useful The math module is used to access mathematical
The range() function is a built-in function in Python
in a while loop as wellas in a for loop. functions in the Python. All methods of this
that helps to iterate over asequence of numbers.
The break statement exits from the loop and functions are used for integer or real type objects,
It works only with integers
transfers the execution fromthe loop to the not for complex numbers.
It produces an iterator that follows arithmetic
statement that is immediately following the loop. To use this module, we should import that module
progression.
The continue statement causes execution to into our code.
range(n) generates a sequence of numbers that
immediately continue at the start of the loop, import math
starts with 0 and ends with (n-1).
Constant of math module
for-else loop Categories of functions
Pi: Return the value of pi: 3.141592
For loop executes the block until a condition is Two categories of functions are existing in Python.
Example
satisfied. When the condition becomes false, the [Link]-in-functions.
import math
statement immediately after the loop is executed. The [Link] defined functions
print("Value of PI:", [Link])
else clause is only executed when your for condition
Output
becomes false. If you break out of the loop, or if an
Value of PI: 3.141592653589793
exception is raised, it won't be executed.
Functions of math module [Link] definition Calling a function
Numeric functions Function definition is used specify name of the Once we have defined a function, we can call it from
[Link](x): Return the Ceiling value. It is the function, arguments and block of statements that another function, program or even from the Python
smallest integer, greater or equal to the number x. will execute when the function is called. prompt. To call a function we simply type the
[Link](x): Returns the absolute value of x. Syntax function name with appropriate parameters. User can
[Link](x): Returns factorial of x. where x ≥ 0 def function_name(parameters): call a function multiple times.
[Link](x): Return the Floor value. It is the largest """docstring""" Parameters and Arguments
integer, less or equal to the number x. statement(s) Parameters and arguments are the values or
[Link](x, y): Find remainder after dividing x return(expression) expression passed to the functions between
by y. 1. Keyword def that marks the start of the function parentheses. Arguments are variables/values passed
2. DateTime functions header. The first line of the function is known as with functions at the time of function calling. The
Module named datetime is used to work with dates. header and the rest of the function is known as body value of the arguments are always assigned to
Functions of the function. The header line must end with a variables and these variables are known as
[Link](): Returns date only. The colon. Add a little bit
parameters. Parameters areof body text
included at the time of
returned date contain year, month, and day. 2. A function name to uniquely identify the function. function definition within function header.
[Link](): Returns date and time. The Function naming follows the same rules of writing Formal Arguments
returned date contains year, month, day, hour, identifiers in Python. Formal arguments are arguments passed at the time
minute, second, and microsecond. 3. Parameters (arguments) through which we pass of function calling.
strftime() Method: The datetime object has a method values to a function. They are optional. Recursion
for formatting date objects into readable strings. The 4. A colon (:) to mark the end of the function header. Recursion is the process of function to call itself.
method is called strftime(), and takes one parameter, 5. Optional documentation string (docstring) to Every recursive function must have a base condition
format, to specify the format of the returned string: describe what the function does. that stops the recursion or else the function calls
6. One or more valid python statements that make itself infinitely. The Python interpreter limits the
𝗟𝗲𝗻 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 depths of recursion to help avoid infinite recursions,
up the function body. Statements must have the
len is a built in function in Python. resulting in stack overflows. By default, the
same indentation level (usually 4 spaces).
When used with a string, len returns the length or maximum depth of recursion is 1000. If the limit is
7. An optional return statement to return a value
the number of characters in the string. crossed, it results in RecursionError
from the function.
◾ The list is a most versatile data type available in A Python set is similar to this mathematical
between them.
The items or elements in a dictionary are separated
Python which can bewritten as a list of comma- definition with some additionalfeatures.
by commas and all the elementsmust be enclosed in
separated values (items) between square brackets. The elements in the set cannot be duplicates.
◾ List are mutable, meaning, their elements can be The elements in the set are immutable(cannot be
curly braces.
A pair of curly braces with no values in between is
changed. modified) but the set as a whole ismutable.
◾ In Python programming, a list is created by There is no index attached to any element in a
known as an empty dictionary.
The values in a dictionary can be duplicated, but the
placing all the items(elements) inside a square python set. So they do not supportany indexing or
keys in the dictionary are unique.
bracket [], separated by commas. slicing operation
◾ It can have any number of items and they may be
of different types(integer, float, string etc.).
◾.Differentiate between Type Conversion and ◾. What are the rules that we need to keep in mind ◾. What is the Role of Indentation in Python?
Type Casting while creating Python variables? The identification of a block of code in Python is
Type Conversion is the conversion of object from There are some rules that we need to keep in mind done using Indentation. Most of the programming
one data type to another data type. Implicit Type while creating Python variables. languages like C, C++, and Java use braces { }
Conversion is automatically performed by the • Name of a variable cannot start with a number. It to define a block of code. Python, however, uses
Python interpreter. Explicit Type Conversion is also should start with either an alphabet or the under- indentation. A code block (body of a function, loop,
called Type Casting, the data types of objects are score character. etc.) starts with indentation and ends with the first
converted using predefined functions by the user. • Variable names are always case sensitive and unindented line
◾ Differentiate between Mutable and Immutable can contain alphanumeric characters and the Tuple Assignment
Types underscore character. It allows the assignment of values to a tuple of
Data are stored in a computer’s memory for pro- • Reserved words cannot be used as variable variables on the left side of the assignment from the
cessing. Some of these values can be modified names. tuple of values on the right side of the assignment.
during processing, but contents of others can’t
be altered once they are created in the memory.
◾ . What are the two categories of statements in Union of Sets:- The union operation on two sets
python? produces a new set containing all the distinct
Numbers, strings, and Tuples are immutable, There are two categories of statements in Python: elements from both the sets.
which means their contents can’t be altered after • Expression Statements Intersection of Sets:- The intersection operation on
creation. On the other hand, items in a List or • Assignment Statements two sets produces a new set containing only the
Dictionary object can be modified. It is possible to With the help of expressions, we perform operations common elements from both the sets.
add, delete, insert, and rearrange items in a list or like addition, subtraction, concatenation etc. In oth- Difference of Sets:- The difference operation on two
dictionary. Hence, they are mutable objects. er words, it is a statement that returns a value. sets produces a new set containing only the
Accessing Elements of a List With the help of assignment statements, we create elements from the first set and none from the
Index operator [] is used to access an item in a list new variables, assign values and also change second set.
Index starts from 0 values. Compare Sets:- We can check if a given set is a
Ages=[32,25,40,18]
subset or superset of another set.
print(Ages[0])
The result is True or False depending on the
Output: 32
elements present in the sets.