Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

Hands-on Workshop

on
Python Coding with Application Projects & Solutions
Hands-on Course
on
Python Coding with Application Projects & Solutions
Complete Hands-on Sessions
Summary :

➔ Built-in Datatypes, int, float, Str & bool


➔ Type casting
➔ Operators - Arithmetic, Conditional, Logical, Membership
➔ Control statements - if else
➔ Loops - for, while
➔ Structured Data types - Lists, Tuples & Dictionaries
➔ Functions - User-Defined, Lambda, Built-in
➔ Modules
Why Coding is
Important?
What is Coding?

People are constantly talking about it

what is it? Is it difficult?


Python Popularity
Programming Python ● Easy to use
Language ● Powerful
● Versatile

Created in 1991 by ● Cross platform


Guido Van Rossum ● Freely useable for
Commercial use
Application Fields
How python is simpler than other languages ?

C Program Python Program


#include <stdio.h> print(‘Hello World’)
int main()
{
printf("Hello, World!");
return 0;
}
Note:

Many in-built
Functions are
present in Python
+
PC Software Installation:
Software Installation Link
PC Software Installation:
Software Installation Link
Android APP Installation:

1. Pydroid 3
Jupyter Notebook:

Click on
“Jupyter
Notebook(Anaconda3)”

On Desktop Search box


Type
“Jupyter Notebook”
Jupyter Notebook: Don’t Close
Cmd Prompt Window,
Just minimize it.
Jupyter Notebook : Create new file
Jupyter Notebook Window:
Code cell
Very First Python Program

Example 1: Output Function

print ('Welcome to Python Workshop')

print is a OUTPUT String to display


function
Python Basics:
What is Variable?

A Python variable is a name given to reserved


memory location to store values.

Example:

a = 100
print(a)

In the above program, variable is


defined as “a” to print the value as 100

a 100
Rules of Defining Variable:
1. No command or Keyword for declaring a variable.
int a = 100

2. It is created when you assign a value to it & it is


a = 100
not necessary to declare the variable data type.

3. The variable must start with a letter or


Ex: David = 1000
underscore. The variable name must not start with a _bill = 2000
number.
Ex:
4. No symbols are used in variables. value$@ = 250
This is not allowed

Ex:
Val = 10
5. Variables are Case-Sensitive val = 100
Note: Val & val are different
Python Built-in Data Types
int (Integer):

They are positive or negative whole numbers with no decimal point.

Number of students in a Number of Vehicles in parking


University area

How we Define? Python program:

a=5
Students = 40
print(type(a))

Variable Integer Output :


Name number <class 'int'>
float:
Decimal numbers comes under float data type.

Temp = 27.52

Python program: 5.1” 5.9”


a = 2.0
print(type(a))

Output:
<class 'float'>
String (str):

Msg = ‘INDIA’

Msg = “INDIA”

Python Program

a = “Python Programming”

print(a)

print(type(a))
Boolean:
It has two boolean values True or False.

Python Program :

a = True False True


print(type(a))

Output :

True
Input & Output Functions

Example code: Input function Output function

Code without input() :

Msg = “Hello World !”


input() print()
print(Msg)

Code with input():

Msg = input()
print(Msg)
Print Function Types:

Type-1: Printing multiple Variables in single Line

Type-2: Print with String & Variable Data

Type-3: Printing variable using tagging


Type Casting: Calculations in Software
Require numerical data

“$get hello,123|^result=”

Cloud Data is in string Format

“G0 X10 Y10 Z5”

Convert the String data into number to


“G0 X10 Y10 Z5” calculate x, y & z & do milling operations
Type Casting:

int float

str

Casting in python is therefore done using constructor functions

● int() - Used to Convert any type of data into int data type
● float() - Used to Convert any type of data into float data type
● str() - Used to Convert any type of data into string data type
Python Type Casting:
Ex-2 Integers:
Ex-2 String:
y = int(2.8) # y will be 2
y = str(2) # y will be '2'
z = int("3") # z will be 3 z = str(3.0) # z will be '3.0'

print(y, z)

Ex-2 Float:
x = float(1) # x will be 1.0

z = float("3") # z will be 3.0


Arithmetic Operators:
Operator Name Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

** Exponentiation x ** y
P-1 Simple Calculator using Arithmetic operators:
a = int(input(“Enter a: ”))
b = int(input(“Enter b: ”))
Sum = a+b
Diff = a-b
Product = a*b
Division = a/b
print(sum)
print(Diff)
print(Product)
print(Division)
P-2 Area of Circle:

Python Code:
diameter = 2
pi = 3.14
Area = pi/4 * (diameter**2)
print(Area)
P-3 Volume and Surface Area of Cylinder:
Problem Statement:

For given radius(r) and height(h), calculate Volume & Surface Area of a Cylinder

Assume, r = 3 & h = 10
P-3 Python Code:

pi = 3.14
r=4
h = 10
volume = pi * r * r * h
Surface_Area = 2 * (pi * r * r) + 2 * (pi * r * h)
print(volume, Surface_Area)
Some important parameters:

Comments: It Start with symbol #, rest of line is ignored

Multiple Assignments:
You can assign to multiple names at the same time.
Ex: x, y = 2, 3

Assignments can be chained. Ex: a = b = x = 2

Easy to swap values. Ex: x, y = y, x


Conditional Statements in Python
Conditional Operators:
Operator Example Same as
== Equal X == Y

!= Not Equal X != Y

> Greater than X>Y

< Less than X<Y

>= Greater than or X >= Y


Equal to
<= Less than or equal X <= Y
to
If statement
Self Driving Car

Slow down

Break ON
ON
If statement
If statement
Syntax:

if (condition):
Application code
Main Code
When Sensor detects,
Motor Pump will ON

Ex: Automatic Sanitizer Dispenser


This is our Application code
Sensor = 1 executed when Hand detects
if (Sensor == 1):
print(“Pump ON”)
This is main code
print("Pump OFF")
Indentation
Ex: ATM card Insert
card = 1
➔ This Tab space or character space
is called indent. if (card == 1):
➔ This tells that the set of instructions print("Do transaction")
with indent is inside if statement
print("Show Advertisements")

Ex: ATM card Insert

card = 1
if (card == 1):
This is an error. print("Do transaction")

print("Show Advertisements")
If else: Syntax:

if (conditions):
Robert Statement 1
John
else:
Statement 2
Age is 6 Age is 5

Ex-3: Python Code


John_age = 6
Robert_age = 5
if John_age > Robert_age :
print("John is elder than Robert")
else:
print("Robert is elder than John")
if elif statements:

1 2

3 4
If elif else: Coffee Machine: Python Code

Syntax:

If (condition-1):
Statement-1
Statement-2
……..
elif (condition-2):
Statement-3
Statement-4
else:
Statement-5
P-4 Comparision of Two Numbers:
Code:

a = 33 Output:

b = 33 a and b are equal

if b > a:

print("b is greater than a")

elif a == b:

print("a and b are equal")

else:

print("a is greater than b")


Nested if else:

Syntax:

if (condition-1):
Statement-1
if(condition_A):
Statement-2 Inner
Outer
if else: if
Statement-3
else:
Statement-4
Nested if else:
Money Given = 0 Money Given < 5000 Money Given >= 5000

Full Bill Not fully


Cleared
Pending
P-4 Credit card Bill Payment:
Slicing Concept:
Hello World

{
ello

a = “Hello World”
index 0 12 ... 10

Slicing a[1:5] In general,

Example code: a[start_index : end_index + 1]

a = "Hello, World!"
b = a[1:5]
print(b)
P-5 Number Plate Recognition: Toll Gate Application

KA 01 1234 AP 02 3456 MH 05 6780

Purpose : Finding respective state of vehicle based on number plate recognition


P-5 Code:
Summary :

Covered Till Now:


➔ Built-in Datatypes, int, float, Str & bool
➔ Type casting
➔ Operators - Arithmetic, Conditional
➔ Control statements

To Be learnt
➔ Logical Operators
➔ Loops
➔ Structured Data types - Lists, Tuples & Dictionaries
➔ Functions
➔ Python Libraries
String Operations:
len():
Built-in Functions
➔ len()
➔ type()
➔ .upper()
➔ .lower() upper() & lower():
➔ .strip()
➔ .replace(a,b)
Logical Operators:

Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

Reverse the result,


not returns False if the result is true
not (x < 5 and x < 10)
‘and’ use case:

Covid-19

> 100 shortness of


breath
‘and’ use case:

Python Code:
‘or’ use case:

Annual income > 5 lac 5 acres of land


‘or’ use case:

Python Code:
AND Operation:

Switch1
Control
System

Switch2
Motor

P-6 Python Code for AND Operation:


Switch1 = True
Switch2 = False

if(Switch1==True and Switch2==True):


print(“Motor is ON”)
else:
print(“Motor is OFF”)
P7 - Two Way Switch Control
OFF
ON Logic Table:
A B Y
0 0 0
0 1 1
1 0 1
1 1 0

OFF
ON
P-7 Code:

Approach-1 Approach-2
P-7 Code:

Approach-3
P-8 Body Mass Index (BMI) Calculator :

BMI = weight (kg) / (height (m) * height (m))


P-8 Python Code:

You might also like