2. INTRODUCTION
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
It uses English keywords frequently where as other languages use
punctuation, and it has fewer syntactical constructions than other
languages.
2
3. Python is Interpreted − Python is processed at runtime by the interpreter. You
do not need to compile your program before executing it. This is similar to
PERL and PHP.
Python is a Beginner's Language − Python is a great language for the
beginner-level programmers and supports the development of a wide range
of applications from simple text processing to WWW browsers to games.
Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
3
4. What can Python do?
Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
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.
4
6. Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be executed
as soon as it is written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
6
7. Python Syntax
Syntax is one of the basic requirements that we must know to code in any
language.
In Python, we use indentation to represent the block of the code. We have
to follow some rules about how to use the indentation.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where 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.
Python will give you an error if you skip the indentation.
The number of spaces is up to you as a programmer, the most common use
is four, 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
7
9. Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("Hello, World!")
9
11. Keywords
Python Keywords are special reserved words that convey a special meaning to the
compiler/interpreter. Each keyword has a special meaning and a specific operation.
These keywords can't be used as a variable.
Following is the List of Python Keywords.
True False None and as
asset def class continue break
else finally elif del except
global for if from import
raise try or return pass
nonlocal in not is lambda
11
13. Python Variables
Variables are nothing but reserved memory locations to store values. This
means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory. Therefore, by
assigning different data types to variables, you can store integers, decimals
or characters in these variables.
Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space.
The declaration happens automatically when you assign a value to a
variable. The equal sign (=) is used to assign values to variables.
13
14. The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable.
For example −
14
15. Python allows you to assign a single value to several variables simultaneously.
For example −
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are
assigned to the same memory location.
You can also assign multiple objects to multiple variables.
For example −
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b
respectively, and one string object with the value "john" is assigned to the
variable c.
Multiple Assignment
15
17. Re-declare a Variable
You can re-declare Python variables even after you have declared once.
Here we have Python declare variable initialized to f=0.
Later, we re-assign the variable f to value “guru99”
17
18. Python Data Types
Variables can hold values, and every value has a data-type.
Python is a dynamically typed language; hence we do not need to define the
type of the variable while declaring it.
The interpreter implicitly binds the value with its type.
a = 5
The variable a holds integer value five and we did not define its type.
Python interpreter will automatically interpret variables a as an integer type.
Python enables us to check the type of the variable used in the program.
Python provides us the type() function, which returns the type of the variable
passed.
18
20. Standard data types
A variable can hold different types of values. For example, a person's name
must be stored as a string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method
on each of them. The data types defined in Python are given below.
Numbers
Sequence Type
Boolean
Set
Dictionary
20
21. Numbers
Number stores numeric values. The integer, float, and complex values belong to a
Python Numbers data-type.
Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc.
Python has no restriction on the length of an integer. Its value belongs to int
Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is
accurate upto 15 decimal points.
complex - A complex number contains an ordered pair, i.e., x + iy where x and y
denote the real and imaginary parts, respectively. The complex numbers like 2.14j,
2.0 + 2.3j, etc.
21
23. Sequence Type
The string can be defined as the sequence of characters represented in the quotation
marks. In Python, we can use single, double, or triple quotes to define a string.
In the case of string handling, the operator + is used to concatenate two strings as
the operation "hello"+" python" returns "hello python".
The operator * is known as a repetition operator as the operation "Python" *2
returns 'Python Python'.
Example - 1
23
25. List
Python Lists are similar to arrays in C. However, the list can contain data
of different types. The items stored in the list are separated with a comma
(,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the list. The
concatenation operator (+) and repetition operator (*) works with the list in
the same way as they were working with the strings.
25
27. A tuple is similar to the list in many ways. Like lists, tuples also contain the
collection of the items of different data types.
The items of the tuple are separated with a comma (,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't modify the size and value of the
items of a tuple.
Tuple
27
29. Dictionary is an unordered set of a key-value pair of items. It is like an associative
array or a hash table where each key stores a specific value.
Key can hold any primitive data type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the
curly braces {}.
Consider the following example.
Dictionary
29
30. Boolean type provides two built-in values, True and False. These values are used to
determine the given statement true or false.
It denotes by the class bool. True can be represented by any non-zero value or 'T'
whereas false can be represented by the 0 or 'F'. Consider the following example.
# Python program to check the Boolean type
Boolean
30
31. Set
Python Set is the unordered collection of the data type.
It is iterable, mutable(can modify after creation), and has unique elements. In set, the
order of the elements is undefined;
it may return the changed sequence of the element.
The set is created by using a built-in function set(), or a sequence of elements is
passed in the curly braces and separated by the comma.
It can contain various types of values.
31
33. Python Operators
The operator can be defined as a symbol which is responsible for a particular
operation between two operands.
Operators are the pillars of a program on which the logic is built in a specific
programming language.
Python provides a variety of operators, which are described as follows.
33
34. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between two
operands. It includes + (addition), - (subtraction), *(multiplication), /(divide),
%(reminder), //(floor division), and exponent (**) operators.
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b =
10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first
operand. If the first operand is less than the second operand,
the value results negative. For example, if a = 20, b = 10 =>
a - b = 10
/ (divide) It returns the quotient after dividing the first operand by the
second operand. For example, if a = 20, b = 10 => a/b = 2.0
* (Multiplication) It is used to multiply one operand with the other. For
example, if a = 20, b = 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the
second operand. For example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the
first operand power to the second operand.
// (Floor division) It gives the floor value of the quotient produced by dividing
the two operands.
34
35. Comparison operator
Comparison operators are used to comparing the value of the two operands and
returns
Boolean true or false accordingly. The comparison operators are described in the
following table.
Operator Description
== If the value of two operands is equal, then the condition
becomes true.
!= If the value of two operands is not equal, then the condition
becomes true.
<= If the first operand is less than or equal to the second
operand, then the condition becomes true.
>= If the first operand is greater than or equal to the second
operand, then the condition becomes true.
> If the first operand is greater than the second operand, then
the condition becomes true.
< If the first operand is less than the second operand, then the
condition becomes true.
35
36. Assignment Operators
The assignment operators are used to assign the value of the right expression to the
left operand. The assignment operators are described in the following table.
Operator Description
= It assigns the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20
=> a+ = b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10
=> a- = b will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10,
b = 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and
assigns the reminder back to the left operand. For example, if a = 20, b = 10 =>
a % = b will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign
4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign
4//3 = 1 to a. 36
37. Bitwise Operators
In Python, bitwise operators are used to performing bitwise calculations on
integers. The integers are first converted into binary and then operations are
performed on bit by bit, hence the name bitwise operators. Then the result is
returned in decimal format.
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is
copied to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the
resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise,
the resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit
is 0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present
in the right operand.
>> (right shift) The left operand is moved right by the number of bits present in
the right operand.
37
38. Logical Operators
The logical operators are used primarily in the expression evaluation to make a
decision. Python supports the following logical operators.
Operator Description
and If both the expression are true, then the condition will be
true. If a and b are the two expressions, a → true, b →
true => a and b → true.
or If one of the expressions is true, then the condition will be
true. If a and b are the two expressions, a → true, b →
false => a or b → true.
not If an expression a is true, then not (a) will be false and
vice versa.
38
39. Membership Operators
Python membership operators are used to check the membership of value inside a
Python data structure. If the value is present in the data structure, then the resulting
value is true otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second
operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second
operand (list, tuple, or dictionary).
39
40. Identity Operators
The identity operators are used to decide whether an element certain class or type.
Operator Description
is It is evaluated to be true if the reference present at both
sides point to the same object.
is not It is evaluated to be true if the reference present at both
sides do not point to the same object.
40
41. Operator Precedence
The precedence of the operators is essential to find out since it enables us to know which
operator should be evaluated first. The precedence table of the operators in Python is
given below.
41