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

3.Python(Components of a Program)

The document outlines the fundamental components of a Python program, including expressions, statements, comments, functions, blocks, and indentation. It explains how to create and manipulate variables, the concept of dynamic typing, and the use of input and output functions. Additionally, it provides examples and practice exercises for better understanding of Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

3.Python(Components of a Program)

The document outlines the fundamental components of a Python program, including expressions, statements, comments, functions, blocks, and indentation. It explains how to create and manipulate variables, the concept of dynamic typing, and the use of input and output functions. Additionally, it provides examples and practice exercises for better understanding of Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Python (Components of a program)

The barebones / structure / components of a program:


 Expressions
 Statements
 Comments
 Functions
 Blocks and Indentation
Expressions
An expression is a combination of symbols that represents a value.
12, 2.4, 5.6 -- expressions that are values only
a+5
(3 *4) / (2*5) ---- expression that produces a value.
Statements
While an expression represents values, a statement is a programming
instruction that does some action.
While an expression is evaluated, a statements is executed (some action
takes place). A statement executed may or may not results a value
x= (3*4) / (2*2)
Comments
Comments are additional readable information, which is read by the
programmer but ignored by Python interpreter. In python comments begin
with the symbol “#”
Multi line comments:
i. Add a # symbol in the beginning of every line
# This is to explain
# multi-line comment.
# using # symbol
ii.Type comment as a triple quotes multi-line string, this type of multi-line
comment is also known as doc string. We can use triple apostrophe('
' ') or triple quotes(" " ")
''' This is to explain
multi-line
comment.
Using triple apostrophe(' ' ') or triple quotes(" " ") '''
Functions
A Function is a code that has a name and it can be reused (executed
again) by specifying its name in the program. Calling a function
becomes a statement. Print is a function ---- print( ) , input( )
print(a+b).
input and print () in python:
input ()- to get user input
print() to print/display the output
int(input())- int() function helps us to input integer value. If any other
type of value is entered it will throw an error.

Blocks and Indentation


Python provides no braces to indicate blocks of code for class and
function definitions or flow control. Blocks of code are denoted by line
indentation, which is rigidly enforced.
The number of spaces in the indentation is variable, but all statements
within the block must be indented the same amount. For example −
b=int(input("enter the value of b"))
if b<5:
print("The value of b is less than 5") # block
print("Thank you")
elif b>5:
print("The value of b is greater than 5") # block
print("Thank you")
else:
print("The value of b is equal to 5") # block
print("Thank you")

However, the following block generates an error –


b=int(input("enter the value of b"))
if b<5:
print("The value of b is less than 5")
print("Thank you")
elif b>5:
print("The value of b is greater than 5")
print("Thank you")
else:
print("The value of b is equal to 5")
print("Thank you")
Thus, in Python all the continuous lines indented with same number of
spaces would form a block.
Variable and assignments
Variable is an identifier whose values can change. Variable name
should be unique in a program. Value of a variable can be string,
numeric etc….
In python, we can use an assignment statement to define or create new
variables and assign specific values to them.
Using an undefined variable in an expression / statements causes an
error called naming error.

print(x)
x=25
print(x)
so to correct the code:
x=0
print(x)
x=25
print(x)

Creating variables
Student_name = 'Arun' # Assignment statement where “=” is the
assignment operator.
Age=16
Percentage=98.5
print(Student_name, Age, Percentage)
Multiple assignments:
1. Assigning same value to multiple variables:
a=b=c=10
2. Assigning multiple value to multiple variables
x,y,z=10,20,’hello’
It will assign the value order wise i.e., first variable is given first
value, second variable second value and so on.
Point to note: x,y=20 will throw syntax error. This type of
assignment is not allowed
Dynamic Typing
A variable pointing to a value of certain type can be made to point to
value of different type by reassigning the value. This is called dynamic
typing. Note: type() help us to understand this
a=10 # numeric value integer
a="welcome" #string value

a="hello"
print(type(a))

a=5
print(type(a))

a = 2.0
print(type(a))

a = 1+2j
print(type(a))

Simple Input and output function statements


Input Statements
To get input from the user interactively, we use the built in function
input ( ). It has a property input( ) always returns string type data
Syntax:
variable_to_hold_the_value=input(<prompt to display>)

Example

name=input("enter your name :")


age=input("enter your age:")
print(name)
print(age)
print("age is of type:",type(age))

Output

enter your name :Radha


enter your age:16
Radha---- #string type data
16------ #string type data
age is of type: <class 'str'>

Reading numbers (input number)


The value entered through input( ) function returns only string type
data. To input (int or float) values, python has two function int( ) or
float( ) to be used with input( ) to convert the value into (int or float)
data type

Example

name=input("enter your name :")


age=int(input("enter your age:"))
print(name)
print(age)
print("age is of type:",type(age))

output

enter your name :Radha


enter your age:16
Radha
16
age is of type: <class 'int'>

type( ) -To determine the type of variable we use type( )

Syntax: type(<variable name>)

Example
a=10
print(type(a))
Output:
<class 'int'>

Output Statements— The print( ) function is used to output data to


standard output device the monitor.

Example

print("hello") # string

print(17.5) # number

print(2+3) # result of a calculation

r=3
print(3.14*(r*r)) # result of a calculation

print("I\'m",12+5,"years old.")

print("hello all \
welcome to class XI") # ‘\’ slash prints the string in the same
line

print("hello all", end=' ') # prints the line with the string specified with
the end argument -- space
print("welcome to python class")

print("hello all", end='\n') # prints the line with the string '\n' denoted
new line
print("welcome to coding class")

print("hello all",end='$') # prints the line with the string specified with
the end argument '$"
print("Have fun coding")
print("My","name","is","Amit",sep="@#") # print the string with the
specified separator argument

print() # prints a blank line

Output

hello

17.5

28.26

I'm 17 years old.

hello all welcome to class XI

hello all welcome to python class

hello all

welcome to coding class

hello all$Have fun coding


My@#name@#is@#Amit

Program : Write a python program to find the sum of two number. The
number should be a user input.
a=int(input("enter the value of a"))
b=int(input("enter the value of b"))
c=a+b
print("First number",a)
print("Second number",b)
print("sum of two number",c)

Code for practice:


1. Write a Python program to find the area of a rectangle given that its
length is 10 units and breadth is 20 units.(L x B).
2. Write a Python program to find the area of a circle. Pi=3.14 ( pi x r x
r)
3. Write a Python code to find the total and average of 3 mark.
4. Write a python program to calculate the Simple interest where
principle, rate of interest and time should be user input.
(Hint: SI=P x R x N
100 )
5. Write a program to input a number and print its first five multiples
6. Write a program to generate the following output:
5
10
9
 Assign value 5 to a variable using assignment operator(=).
Multiply it with 2 to generate 10 and subtract 1 to generate 9
 Modify the above code so as to print output 5@10@9
Predict the output of the following codes
i. x=10
y, y=x+5,x+2
print(x,y)

ii. x,y=7,2
x,y,x=x+1,y+3,x+10
print(x,y)

iii. a,b=2,6
a,b=b, a+2
print(a,b)

iv. a,b=2,6
print(print(a+b))

v. x,y,z=10,20,3
p,q,r=z-5, x+3, y-4
print('x,y,z:',x,y,z)
print('p,q,r:',p,q,r)

vi. x=10
x=x+10
x=x-5
print(x)
x,y=x-2,22
print(x,y)

Find the error in the program


i. name="Priya"
print("Greetings!!!")
Print("Hello",name)
print(How do you do?")

ii. c=input("enter your class")


Print("last year you were in class"),c-1
print("Best wishes for your future')

You might also like