0% found this document useful (0 votes)
2 views45 pages

Introduction To Python - Lab Exam Preparation

The document outlines the fundamental steps of programming, including algorithms, flowcharts, coding, and output. It explains the importance of algorithms in problem-solving, the graphical representation of algorithms through flowcharts, and introduces programming concepts such as syntax, variables, data types, and control statements. Additionally, it provides examples and exercises related to these concepts to reinforce understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views45 pages

Introduction To Python - Lab Exam Preparation

The document outlines the fundamental steps of programming, including algorithms, flowcharts, coding, and output. It explains the importance of algorithms in problem-solving, the graphical representation of algorithms through flowcharts, and introduces programming concepts such as syntax, variables, data types, and control statements. Additionally, it provides examples and exercises related to these concepts to reinforce understanding.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Session 2

Steps of programming
• Algorithm
• Flowchart
• Coding
• Output
Algorithm
• Writing a logical step-by-step method to solve the problem is called
the algorithm. In other words, an algorithm is a procedure for solving
problems. In order to solve a mathematical or computer problem, this is the
first step in the process.
• An algorithm includes calculations, reasoning, and data processing.
Algorithms can be presented by natural languages, pseudocode, and
flowcharts, etc.
Example
Write an algorithm for adding two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable
sum.
Step 5: Display sum
Step 6: Stop
Flowchart
• With algorithms, we can easily understand a problem.
• A flowchart is the graphical or pictorial representation of an
algorithm with the help of different symbols, shapes, and arrows
to demonstrate a process or a program.
• main purpose of using a flowchart is to analyze different
methods.
Flowchart Convention
Questions
1. What is an Algorithm?
2. List down the shapes used to draw flowchart and its
significance in flowchart.
3. What are the characteristics an algorithm must possess to
qualify?
Test
• Draw the symbols of the flowchart and write their names and
uses.
• Write an algorithm and draw the flowchart to find the square
root of a number.
Test
• Define Algorithm.
• How do we qualify the algorithm as a good algorithm?
• Draw the symbols of the flowchart and write their names and
uses.
Questions:
1. Write an algorithm and draw the flowchart to convert the length
in feet to meter. (1 meter = 3.2 foot)
2. Write an algorithm and draw the flowchart to calculate the area
of a cone.
Variables
• A variable is a named location used to store data in the
memory.
• Think of variables as a container that holds data which can be
changed later throughout programming.
Session 3

syntax
• Basic component of any programming language
• Defines how you should write the statements in a program
• Set of rules that tell you what combinations of symbols that are
correctly structured statements or expressions in that language.
• Differs from each programming language
Statements in programming language
• Every line of program is a statement.
• Every statement must follow the syntax specified to them.
• Syntax is unique to each programming language.
• If any violation, the interpreter throws a syntax error.
Comment line in a programming language

• Programmer readable explanation or annotation in the source


code of a computer program.
• Additional readable information to clarify the source code.
• These are generally ignored by compilers and interpreters.
Comment in Python
• Ignored by Python interpreter
• Begins with # (hash character) and end with end of physical line
• Single line comment
 Full line comment
For Example: #This program shows a program's components
 Inline comment
For Example: if b<5: #colon means it requires a block
• Multi-line comment
Otherwise called as block comment
 Add a # symbol in the beginning of every physical line
Ex:#multi-line comments are useful for additional information
#Related to the program in question
#It helps clarify certain important things
Type comment as a triple-quoted multi line string
Ex: ''' Multi-line comments are useful for detailed additional
information related to the program. It helps
clarify certain important things.
'''
• Also known as docstring
• Can either use (''') triple-apostrophe or triple quotes (""")
Keywords
• Is a word having special meaning reserved by programming
language
• Convey a special meaning to the language interpreter
• Reserved for special purpose and must not be used as normal
identifier names
Keywords in Python
Tokens
# A sample program
A string value or string literal
name = “Anya”
A integer value or integer literal
age = 15
A floating-point value or floating-point literal
contribution = 710.75
print(name, “aged”, age, “has contributed”, contribution)

Keyword
Punctuator Characters

Individual tokens, together make components of a program such as statements of expressions etc.
Assignment statement
Syntax: variable=value
a=5 #is an assignment statement
Where you assign a value to the variable which would be a
constant value.
We equate a variable to a value is an assignment statement
Data types
• Numerical -Integer (int), float, Complex numbers
• Sequence – String, List, Tuple
• Boolean (True/false)
• All the values if not specified will be considered as string value
in Python.
Typecasting
Typecasting the input to Integer: There might be conditions when you might require
integer input from user, the following code takes two input(integer/float) from
console and typecasts them to integer then prints the sum.

# input
num1 = int(input())
num2 = int(input())

# printing the sum in integer


print(num1 + num2)
Typecasting the input to Float: To convert the input to float the
following code will work out.

# input
num1 = float(input())
num2 = float(input())

# printing the sum in float


print(num1 + num2)
Typecasting the input to String: All kind of input can be converted to
string type whether they are float or integer. We make use of keyword
str for typecasting.

# input
string = str(input())

# output
print(string)
Printing Statement
• Text
• Printing variables
• Printing instructions
• Printing results of arithmetic operations
print statement
print( ) is an output statement

Syntax: print(“_________”)
Line of text to be displayed on screen
Printing Text
print(“This is my first code”)
Output – This is my first code
\n – backslash n will continue in the next line
print(“This is my \n first code”)
Output – This is my
first code
Printing variable
Syntax: print(variable)
Example:
a=5
print(a)
Output : 5
Printing results of Arithmetic operations
Print statement can perform all types of arithmetic operations inside its function.

Eg: print(10+20)
or
a=10
b=29
print(a+b)
print(a*b)
print(a/b)
print(a-b)
Print instruction
Ex:1 a=5
b=8
print(a+b)
Ex:2 a=10
print(type(a))
Input statement
input ( ) : This function first takes the input from the user and
then evaluates the expression, which means Python
automatically identifies whether user entered a string or a
number or list.
If the input provided is not correct then either syntax error or
exception is raised by python.
How the input function works in Python :

• When input() function executes program flow will be stopped


until the user has given an input.
• The text or message display on the output screen to ask a user
to enter input value is optional i.e. the prompt, will be printed on
the screen is optional.
• Whatever you enter as input, input function convert it into a
string. If you enter an integer value still input() function convert it
into a string. You need to explicitly convert it into an integer in
your code using typecasting.
syntax
variable = input(“xxxxxxx xx”)

Explicit typecasting
Variable = int(input(“xx xxxx xx”))
Example
# Taking input from the user
name = input("Enter your name")
print("Hello", name)

Output
Hello name #name that the user gives
Example
a=int(input("Enter the number"))
area=a**2
print(area)

(or)
a=int(input("Enter the number"))
area=a*a
print(area)
If statement
• Decision making statement
• Used to decide whether the block of statement will be executed
if it is true.
Syntax:
if(condition):
# statement to execute
Age=int(input(“Enter your age”))
if (Age>=18):
print(“You are eligible to drive”)
If – else statement
Syntax:
if(condition):
statement to execute
else:
statement to execute
Write a python program to check if a number is even or odd
taking the number from the user.
num=int(input(“Enter the number”))
if(num%2==0):
print(“The number is Even”)
else:
print(“The number is odd”)
If-elif-else statement
• Syntax
if(condition1):
expression1
elif(condition2):
expression2
else:
expression3
# program to check the number is positive, negative or
zero

a=int(input("Enter the number"))


if(a==0):
print("The entered number is zero")
elif(a>0):
print("The entered number is positive")
else:
print("The entered number is negative")
Write a Python program to check the place of visit in a particular
city given by the user.

City Place of visit


Delhi redfort

Mysore Mysore Palace

Agra Taj Mahal

Jaipur Jal Mahal

Other cities Not Applicable


a=input("Enter the City: ")
if a=="Delhi":
print("The monument in Delhi is Redfort")
elif a=="Agra":
print("The monument to visit is Tajmahal")
elif a=="Jaipur":
print("The monument to visit in Jaipur is Jai
Mahal")
z = int(input(“Enter the number”))
if z % 2 == 0:
print("z is divisible by 2")
elif z % 3 == 0:
print("z is divisible by 3")
else:
print("z is neither divisible by 2 nor by 3")
Expected VIVA Questions

1. Based on Syntax
2. Definitions
a. Operators
b. Datatypes
c. Syntax
d. Keywords
e. Type casting
f. Algorithm
g. Flowchart & shapes
h. Variables
i. Statement
j. Tokens
k. Comments – single-line, In-line, Multi-line
3. Examples of all the above
4. Drawing flowchart

You might also like