Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Python 5

This document is an introduction to Python covering various topics including data types, variables, conditionals, loops, functions, and file handling. It provides a structured overview of Python concepts with examples and explanations of operators, escape characters, and import statements. Additionally, it includes resources for further learning and hands-on practice.

Uploaded by

janavikadam124
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python 5

This document is an introduction to Python covering various topics including data types, variables, conditionals, loops, functions, and file handling. It provides a structured overview of Python concepts with examples and explanations of operators, escape characters, and import statements. Additionally, it includes resources for further learning and hands-on practice.

Uploaded by

janavikadam124
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Python

Session 5
What will you learn ?

• Introduction • Literals
• Introduction to Data Sets • Conditionals
• Concepts of Variables, Iterations & • Loops
Filtering • Functions
• Data Types • Lists & Tuples
• Sanity of Data • Sets & Dictionaries
• Introduction to Complex Data Types • File Handling
• Hands – On Python • Object Oriented Programming
Summary till date

Phase 1 Phase 2 Phase 3 Phase 4 Phase 5


1. print() 1. Strings 1. while loop 1. Functions 1. File handling
2. Variables 2. Types of 2. for loop 2. Types of 2. Pandas' library
3. input() quotes and 3. Formatted functions 3. Object Oriented
4. Data types Escape printing 3. Types of Programming
5. Operators characters 4. Nested loops function 4. NumPy library
6. Expressions 3. if-elif-else 5. break, arguments 5. Matplotlib
4. Types of continue, 4. Scope of library
import pass variable
statements
Summary Till Date

Datatypes
In Python every entity is
an object including all Variables
datatypes
No need to declare variables in Python. It is
Dictionary Set Boolean created when the value is assigned to it
Numeric Sequence
dict() set() bool() first time. Python allows dynamic typing.
Variable name is any sequence of
Integer String characters a-z, A-Z, 0-9 and underscore but
int() str() not starting with numbers. Variables are
case sensitive.
Float List
float() list()

Complex Tuple
complex() tuple()
Summary Till Date - Operators
Operators Symbol / Keyword Name / Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Arithmetic // Floor division
** Exponential
== Double equal to
!= Not equal to
> Greater than
< Less than
Comparison >= Greater than equal to
<= Less than equal to
and Returns True if both statements are true
or Returns False if one of the statements are True
Logical not Reverse the result
in Returns True if the value is present in the sequence
Membership not in Returns True if the value is not present in the sequence
is Returns True if both variables point to the same object

Identity is not Returns True if both variable do not point to the same object
Escape characters

Backslash ‘\’ is considered as escape character in Python.


It is used to insert characters in strings which are illegal
otherwise.
Escape character Result
\\ Backslash
\' Single quote
\" Double quotes
\n New line
\t Tab
Types of import statements

import math
It will import the math library
from math import *
It will import the entire contents of math library
from math import pi
It will import only variable pi from math library
import calendar as cal
It will import the calendar library and it can be accessed as cal
from calendar import month as m
It will import only month method from calendar library and it
can be accessed as m
Conditional Statements

Conditional statements are used when some kind of


decision making is required.

Comparison operators are most useful when we use these


conditional statements.
If Block

if block:
The goal of this statement is to check if the given condition is
True or False.
If it is True, then Python will execute the following indented
lines of code.

i f a > b:
p r i n t ( ' a i s greater than b ' )
If Example

print("enter your date of birth")


date_birth=int(input())
current_year=2022
age=current_year-date_birth
if(age<13):
print("you are underage, cannot watch this movie")
else:
print("awesome you can enjoy the movie")
if – elif blocks

if – elif blocks:
elif stands for else if.
This is Python’s way of saying, if the first condition is False
then it will check the next condition.

i f a > b:
p r i n t ( ' a i s greater than b ' )
e l i f a < b:
p r i n t ( ' a i s less than b ' )
If Else, Elif
Problems

To find the given no's are even or odd

Sr.No Number Expected Output

1 4 Even
2 6 Even
3 1931 Odd
4 -45 Odd
5 44 Even
if – elif blocks

if – elif – else blocks:


If all the conditions given in if and/or elif evaluates to be False,
then Python executes else block.

i f a > b:
p r i n t ( ' a i s greater than
e l i bf ' a) < b:
p r i n t ( ' a i s less than b ' )
else:
p r i n t ( ' a and b are equal')
Nested Conditional Statements

Nested conditional statements:


Any of the above-mentioned conditional blocks can be
written inside any other conditional blocks and such kind of
structure is referred as nested if – else.
For V/s While Loops
Loops
Nested loops:
Any of the above-mentioned loops (while and 2 versions of for) can be written inside any of these loops and such
kind of structure is referred as nested iterations/loops.

Loop control statements:


Loops are used to automate repetitive statements.

But sometimes there may arise a condition where we may want to terminate the loop, skip an iteration or ignore
that condition. In such conditions we use loop control statements like break, continue and pass respectively.
Range

range(start, end, step):


It is an in-built function which returns a sequence of numbers based on the values of
start, end and step.
 start: An integer number specifying at which position to start.
 It is an optional argument. Default value is 0.
 end: An integer number specifying at which position to stop (non-inclusive).
 step: An integer number specifying the incrementation/decrementation.
 It is an optional argument. Default value is 1.
While Loop
while loop:
It enables you to execute set of statements as long as the given
condition is True

i =0
while i < 10:
i += 1

This code will print numbers from 0 to 9


For Loop

1. Using range():
for i in range(10):
pr i nt ( i )
This code is equivalent to the above while loop and it will also print numbers from 0 to 9

2. Without using range():


f o r i i n ' H e l l o a l l . Welcome to Python':
pr i nt ( i )
While Loop

while loop:
It enables you to execute set of statements as long as the given
condition is True.

i =0
while i < 10:
pr i nt ( i )
i += 1
This code will print numbers from 0 to 9.
Resources

• https://drive.google.com/drive/my-drive

• https://www.python.org/

• https://www.python.org/psf/

• http://www.replit.com/

• https://docs.replit.com/tutorials/introduction-to-the-repl-it-ide

You might also like