2. Agenda
• Brief History of python
• What is python
• Why python
• Comments
• Variables and data types
• Typecasting in python
• Loops in python
• Functions in python
• Lists in python
• Tuples in python
3. Brief History of
Python
• Invented in the Netherlands, early 90s by Guido van Rossum
• Named after Monty Python
• Open sourced from the beginning
• Considered a scripting language, but is much more
• Scalable, object oriented and functional from the beginning
• Used by Google from the beginning
• Increasingly popular
4. What Is Python?
• Python is dynamically typed, General Purpose Language that
supports an object oriented programming as well as a functional
programming approach
• Python is an interpreted and a high level language, It is easy to
learn and it has efficient high level data structure
• Python comes with a large library of standard modules
5.
6. Comments
A Comment is the part of coding file that programmer does not want
to execute, rather the programmer uses it to either explain the block
of the code or to avoid the execution of a specific part of code while
testing.
Single line comment
To write a comment just add ‘#’ at the start of the line
Example: # this is a single line comment
Print(“this is a print statement”)
Output: this is a print statement
7. Multiline comments
• To write a multi line comments we can use # at starting of each line
or we can use the multiline string
• Example:
“““This is an if-else statement.
it will execute a block of code if a specified condition is true.
if the condition is false then it will execute another block of
code. ”””
8. Variables and Data types
• Variables: Variable is like a container that holds data. Very similar to how our
containers in kitchen holds sugar, salt etc. creating a variable Is like creating a
placeholder in memory and assigning it some value.
for example
a=5678
print(a)
b=“Akash”
print(b)
Output : 5678
Akash
So here a and b are variables and 5678 and Akash are data types
9.
10. Typecasting in python
The conversion of one data type into the other data type is known as
typecasting in python or type conversion in python.
Python supports a wide variety of functions or methods like: int(),
float(), str(), ord(), hex() , oct(), tuple(), set(), list(), dict(), etc. for type
casting in python.
Two types of typecasting
1. Explicit typecasting
2. Implicit typecasting
11. 1. Explicit typecasting
The conversion of one datatype into another datatype, done via developer or
programmer’s intervention or manually as per the requirement , is known as
explicit typecasting.
Example: a=“1”
b=“2”
print(int(a) +int(b)) Output : 3
2. Implicit typecasting
According to the level, one data type is converted into other by the python
interpreter itself (automatically) . This is called implicit typecasting.
Example
c=1.9
d=8
Print(c+d) output: 9.9
12. Loops in python
• Sometimes a programmer wants to execute a group of statements a number of times. This can
be done using loops. Based on this loops are further classified into following types; for loop,
while loop.
For loop
For loop can iterate over a sequence of iterable object in python. Iterating over a sequence is
nothing but iterating over string, list,tuple , sets, dictionaries.
Colors=[“Red”, “Green”, “Blue”, “Yellow”]
for color in colors
Print(color)
Output: Red
Green
Blue
Yellow
13. While loop
As the name suggest, while loop execute statements while the condition is true.
As soon as the condition becomes false, the interpreter comes out of the while
loop.
Example: count=5
while( count > 0 )
print(count)
count= count-1
Output=5
4
3
2
1
14. Functions in python
• A function is a block of code that performs a specific task whenever it is
called. In bigger programs where we have large amounts of code , it is
advisable to create or use existing functions that make the program flow
organized and neat.
• There are two types of functions
1. Built-in functions
2.User-defined functions
Built-in functions
These functions are defined and pre-coded in python. Some examples of
built-in functions are as follows:
min(), max(), len(), sum(), type(), range(), dict() , list(), touple(), set(), print()
etc.
15. • User-defined functions
• We can create functions to perform specific tasks as per our needs. Such
functions are called as user defined functions
• Syntax
def function_name (Parameter) :
pass
• Example:
def calculateAmean( a, b)
mean=(a+b) /2
print(mean)
16. Lists in python
• Lists are ordered collections of data items.
• They stores multiple items in a single variable.
• List items are separated by commas and enclosed within square brackets.
• List are changeable meaning we can alter them after creation.
Example:
list1=[1, 2,3,4,5,6]
list2=[“Red”, “Green”, “blue”]
print(list1)
print(list2) outputs-[1, 2,3,4,5,6]
[“Red”, “Green”, “blue”]
17. Tuple in Python
• Tuples are ordered collection of objects. They store multiple items in
a single variable. Tuple items are seperated by commas and enclosed
within round brackets() . Tuples are unchangeable meaning we
cannot alter them after creation.
• Example
tuple1=(1, 2,2,6)
print(tuple1)
Output (1, 2,2,6)