PYTHON
PYTHON
INTRODUCTION
● Python is a popular programming language. It was created by Guido Van Rossum and
was released in 1991.
● It is used for
Web development (server-side)
Software development
Mathematics
System scripting
FEATURES OF PYTHON
● It can run on almost any platform including WINDOWS, LINUX, MAC OS X, UNIX etc
print() function
time.sleep() function
Indentation - It refers to the spaces at the beginning of a code line. In other programming
languages, the indentation in code is for readability only, the indentation in Python is very
important. Python uses indentation to indicate a block of code.
TURTLE
Turtle is nothing, but a way to draw with Python script. With the turtle module, one can open a
screen and see the output.Turtle was introduced in 1967 via a language called logo.
We need three commands to launch the turtle screen ( from turtle import, turtle=turtle,
screen=screen )
INPUT FUNCTION
Inputs are a form of data that is accepted from the user or other external environment. The
inputs once taken are processed, then generate a corresponding output.
INPUT DEVICES
The devices which allow us to take input from the user are known as Input devices, for example
keyboard, mouse, etc…
DATA TYPES
Data types are nothing, but that tells a computer how we want to use the data entered. The user
input captures the number of strings (a sequence of characters), and first are defined and then
only they become ready to execute
BASIC DATA TYPES
● String - a sequence of characters including punctuation, numbers and letters.
● Integer - a number with no decimals points
● Float - a number with a decimal point
POINTS TO REMEMBER
● By default the input function considers the variables as string data type
● Strings should always be written in double quotes ( “ “ )
● If x = 3.23 then the int (x) = 3
● Integer short form int
● String short form str
● There are 2 types of print - Print a string: print(“hello world”) and print a variable:
print(variable name)
VARIABLE
● A variable is a reserved memory location to store values.
● In other words, a variable in a program gives data to a computer for processing.
● The value stored in a variable can be changed during program execution.
● Python has no command for declaring a variable
● A variable is created the moment you first assign a value to it
● Variables don't need to be declared with any particular type, and can even change its
type after they have been set
USES OF VARIABLES
● In software programming variables are used to hold 1 or more values.
● Calculations
● Database call
● Game attributes like score, lifes, etc…
● Database query
● Maintaining records
LOGICAL OPERATORS
● == : values must be equal
● != : values must not be equal
● < : left value is less than right value
● <= left value is less than or equal to right value
● > left value is greater than right value
● >= left value is greater than or equal to right value
POINTS TO REMEMBER
● Addition of integer - operator to add 2 integers is in math block
● Addition of string - operator to add 2 strings is in statement block
● Print the string - to print a string, use print hello world from the statement block
● Print a variable - to print a variable ue print variable for the statement block
For loop has a more rigid syntax While loop has a looser syntax
There are 3 steps in for loop (initialization, While loops are executed ass long as the
expression, increment) test expression evaluates to be true
All the above mentioned flow controls are also called as control statement
IF STATEMENT
● The program evaluates the test expression and will execute the statements only if the
test expression is true
● If the test expression is false, the statement is not executed
● In python, the body of the if statement is indicated by the indentation. The body starts
with an indentation and the first unindented line marks the end
● Python interprets non 0 values as true. None and 0 are interpreted as false
● Syntax -
if test expression:
statement(s)
IF… ELSE STATEMENT
● The if…else statement evaluates the test expression and will execute the body of if only
when the test condition is true
● If the condition is false, the body of else is executed. Indentation is used to separate the
blocks
● Syntax:
If test expression:
body of if
else:
body of else
IF…ELIF…ELSE STATEMENT
● The elif is short for else if. It allows us to check for multiple expressions
● If the condition for if is false, it checks the condition of the next elif block and so on
● If all the conditions are false, the body of else is executed
● Only one block among the several if…elif…else blocks is executed according to the
condition
● The if block can have only one else block. But it can have multiple elif blocks
● Syntax:
If test expression:
body of if
elif test expression:
body of elf
else:
body of else
FUNCTIONS
● A function is a block of code which only runs when it is called
● You can pass data, known as parameters into a function
● A function can return data as a result
● In python, a function is a group of related statements that performs a specific task
SYNTAX
def function_name(parameters):
statement(s)
● Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable
● Furthermore, it avoids repetition and makes the code reusable
DOCSTRING
● The first string after the function header is called the docstring and is short for
documentation string. It is briefly used to explain what a function does
RETURN STATEMENT
The return statement is used to exit a function and go back to the place from where it was
called.
SYNTAX
Return[expression_list]
● This statement can contain an expression that gets evaluated and the value is returned
● If there is no expression in the statement or the return statement itself is not present
inside a function, then the function will return the none object
CREATING A FUNCTION
In python, a function is defined using the def keyword
CALLING A FUNCTION
To call a function, use the function name followed by parenthesis
ARGUMENTS
● Information can be passed into functions as arguments
● Arguments are specified after a function name, inside the parenthesis. You can add as
many arguments as you want, just separate them with a comma
WHY ARE FUNCTIONS USEFUL?
● They are subroutines, small sequences of code inside the main code
● We can call the function, and come out of the main code, do the function, then again
come back to the code
● They enable us to reuse sections of code
● They keep our code tidy, and with fewer lines to write
PARAMETERS OR ARGUMENTS?
The terms parameter and argument can be used for the same thing - information that is passed
into a function. From a functions perspective:
● A parameter is the variable listed inside the parenthesis in the function definition
● An argument is the value that is sent to the function when it is called
LISTS
● List’s are the most versatile data storage in python
● They are used to store data and that data can be changed based on different list
properties.
● In python, list is created by placing all the items in square bracket and separated by
commas
● It can have any number of items of different data types
LIST INDEXING
● List indexing is done in such a way that the first element that enters the list takes the 0
index. The elements of listing are denoted in the given way Listname[index] = element.
● Lists are heterogeneous in nature (the items can be of different data types).
● Forward indexing: 0 to n-1
● Backward/Negative indexing: -1 to -n
OPERATIONS ON LIST
● list.append() - to add an element at the end of the list
● list.extend() - to add another list inside the list
● list.pop() - to remove the element from the given position using its index number
● list.remove() - to remove the particular element from the list
● list.insert() - to insert the later element at the position shown as the first element
LOGICAL OPERATORS
The logical operators in python are used to combine the true or false values of the variables so
you can figure out the resultant value
OR LOGICAL OPERATOR(||)
If anyone of the conditions are true, program can be further executed
COMMENTS(#)
● In computer programming a comments is a programer readable explanation on
annotation in the source code of a computer program
● Comments in python began with a hash mark and white space character and continue to
the end of the line
● Because the comments don't execute, when you run a program you will not see any
indication of a comments there
● Comments are in a source code for the humans to read, not for the computers to
execute
● There are two types of comments: single line comment and multiline comment
● A single line comment starts and end in the same line
● Python doesn't have a separate way to write multiline comments. However, we can use
hash at the beginning of each line of a comment on multiple lines
ALGORITHM
● An algorithm is a procedure or formula for solving a problem, based on conducting a
sequence of specified actions
INDENTATION
● It refers to the spaces at the beginning of the code
● Python uses indentation to indicate a block of code
● Python will give you an error if you skip the indentation
● The number of spaces is up to you as a programmer, but it has to be at least one
● You have to use the same number of spaces in the same block of code, otherwise
python will give you an error
BINARY NUMBER
A number system where a number is represented by using only two digits (0,1) with the base 2
is called as a binary number system
DECIMAL NUMBER
A decimal number system is also known as the base 10 numeral system. It uses 10 digits from
0-9.
DATA TYPES
Data types are defined as the data storage format that a variable can store to perform a specific
operation
STRING OPERATIONS
1. Concatenation() - adds values on either side of the operator(+) and returns both the
strings as one
2. Repetition() - creates new strings, concatenating multiple copies of the same string
3. Slice() - gives the character from the given index
4. Range slice() - gives the character from the given range
5. Membership() - returns true if a character exists in the given string
6. Membership() - returns true if a character doesn't exist in the given string
ARITHMETIC OPERATORS
1. Addition (+) - adds two operands
2. Subtraction (-) - subtracts right operand from the left
3. Multiplication (*) - multiplies two operands
4. Division (/) - divides left operands from the right
5. Modulus (%) - remainder of the division of left operand by the right>
6. Floor division (//) - division that results into a whole number
7. Exponent (**) - left operand raised to the power of right
COMPARISON OPERATORS
1. Greater than (>) - true if left operand is greater than the right
2. Less than (<) - true if left operand is lesser than the right
3. Equal to (=) - true if both operands are equal
4. Greater than equal to (>=) - true if left operand is greater than equal to the right
5. Less than equal to (<=) - true if left operand is lesser than equal to right
6. Not equal to (!=) - true if operands are not equal
LOGICAL OPERATORS
1. And (&&) - true if both operands are true
2. Or (||) - true if anyone of the operand in true
3. Not (!) - true if operand is false (complements the operand)
BITWISE OPERATORS
Bitwise operators act on operants as if they were a string of binary digits. They operate bit by
bit, hence the name
IDENTITY OPERATORS
FOR LOOP
● For loop is used for iterating over a sequence (a list, tuple, dictionary, set or a string)
● Syntax:
for val in sequence:
body of for
● Here, val is the variable that takes the value of the item inside the sequence on each
iteration
● Loop continues until we reach the last item in the sequence =
● The body of for loop is separated from the rest of the code using indentation
RANGE FUNCTION
● W-e can generate a sequence of numbers using range functions. Range(10) will
generate numbers from 0 to 9 (10 numbers)
● Syntax: range(x)
● We can also define the start, stop and step signs as range(start, stop, stepsize)
● Step Size defaults to 1 if not provided
● This function does not store all the values in memory: it would be inefficient. So it
remembers start, stop, step size and generate the next number on the go
● To force this functions to output all the items, we can use the function list
SYNTAX
for val in sequence:
body of for
else:
Body of else
● The break keyword can be used to stop a for loop, in such cases, the else part is ignored
● Hence, a for loops else part runs if no break occurs
WHILE LOOP
The while loop in python is used to iterate over a block of code as long as the test expression is
true. We generally use this loop when we don't know the number of times to iterate before hand
SYNTAX
while test_expression:
body of while loop
● In while loop, test expression is checked first the body of the loop is entered only if the
test expression evaluates to true
● After 1 iteration, the test expression is checked again. This process continues until the
test expression evaluates to false
FLOW CHART
● In python, the body of while loop is determined through indentation
● The body starts with indentation and the first unindented line marks the end
● Python interprets any non zero value as true. None and 0 are interpreted as 0
SYNTAX
while test_expression:
body of while
else:
body of else
● The while loop can be terminated with a break statement. In such cases the else part is
ignored
● Hence, a while loops else part runs if no break occurs and the condition is false
BREAK STATEMENT
With the break statement, we can stop the loop even if the while condition is true
CONTINUE STATEMENT
With the continue statement, we can stop the current iteration and continue with the next