0% found this document useful (0 votes)
140 views

CHAPTER 8 INTRODUCTION TO PROGRAMMING-print

Introduction to programming

Uploaded by

kumar266anil
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)
140 views

CHAPTER 8 INTRODUCTION TO PROGRAMMING-print

Introduction to programming

Uploaded by

kumar266anil
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
You are on page 1/ 46

CHAPTER 8

INTRODUCTION TO PROGRAMMING

 Chapter Focus
 Computer Languages
 Python
 Installing Python
 Input and Output
 Data Types
 Operators
 Some More Programs
 Language Translator
 Features of Python
 Programming in Python
 Variables in Python
 Rules for Writing the Variable names
 Comments in Python
 Precedence of Operators

 Let's Begin

State whether the given statements are true or false.

1. Algorithm is a set of instructions for solving a problem


or accomplishing a task. _____________
2. Flow chart is a diagram that illustrates the workflow
required to complete a task or a set of tasks. _____________
3. Flowcharts are useless in analyzing, designing,
documenting, or managing a process or program in
different fields. _____________

In the previous chapter, you have already learnt that an Algorithm is a set of
instructions for solving a problem or accomplishing a task i.e. It’s a step-by-
step procedure that needs to be followed to complete a task in the best way
possible. In this chapter we will be learning about the computer languages
used to develop programs.

 COMPUTER LANGUAGES

Computer is a machine that performs calculations and operations based on


instructions provided by software and hardware program. But, since
computer cannot understand human language, some special languages are
used to provide them instructions called programming languages. Examples
of some programming languages are C++, Java, PHP, Perl, and Python. All
these programming languages are used to write programs out of which
Python is most popular and easy to use.

A computer program is a set of instructions written in a programming


language. The process of writing a program is called programming. A person
who write the programs are called programmer. Computer languages have
been classified into two categories:

Computer Languages

Low-Level Languages High-Level Languages

Machine Assembly Third Fourth Natural


Languages Language (2GL) Generation Generation Language (5GL)
(1GL) Language (3GL) Language (4GL)

 Low Level Language (LLL)

The languages which use only primitive operations of the computer are
known as low level language. It is machine independent language. A low-level
language, often known as a computer’s native language, is very close to
writing actual machine instructions. Low-level languages can be divided into
two categories:
 Machine Language:

Machine language is the only language understood by the computers. Machine


languages are almost impossible for humans to use because they consist of
only binary numbers i.e. 1s and 0s. The bit ‘1’ represents ‘True’ or ‘ON’ state
and ‘0’ represents ‘False’ or ‘OFF’ state. It’s also known by the name first
generation language (1GL).

 Assembly Language:

An Assembly Language is basically Mnemonics like Go, HALT, JUMP, NOT


code which is translated to machine language by programming language
translator called Assembler. Assembly language is also known by the
name second generation language (2GL). Assembly language allows the
programmer to use abbreviations instead of complete words such as ‘A’ for
addition, ‘C’ for Compare, ‘M’ for multiply, etc. Due to this reason it is also
called as ‘Symbolic Language’.

 High Level Language (LLL)

A high-level language (HLL) is a programming language is that enables a


programmer to write a program that are more or less independent of a
particular type of computer. Examples of high languages are: Python, C,
FORTRAN, or Pascal. These languages are known as high-level languages
because they are closer to human languages and further from machine
languages. The main advantage of high level language is because it is easier to
read, write and maintain. Let us learn about some high-level languages.

 Third Generation Languages (3GL)

A third generation programming language or 3GL is a grouping of


programming languages that introduced significant enhancements to second
generation languages. It is done on a purpose to make the programming
language more programmer-friendly.

Common known 3GLs are: COBOL, C++, JAVA, BASIC.

 Fourth Generation Languages (4GL)

The fourth generation programming language or non-procedural language,


often abbreviated as 4GL, enables users to access data in database. These are
very high-level languages are often referred to as goal-oriented
programming languages because they are usually limited to a very specific
application and it might use syntax that is never used in other programming
languages.

4GLs are advantageous as they require less time and effort on the part of the
programmer. Even they are so easy to use that even the programmers
possessing very little knowledge programming can develop programs.
Examples of 4GL are SQL, Perl, Python, etc.

 Natural Languages (5GL)


A fifth-generation programming language or 5GL is a programming language
based on solving using constraints given to the program, instead of using an
algorithm written by a programmer. Examples of 5GLs are Mercury, OPS5,
and Prolog. 5GLs are designed to make the computer to solve the given
problem without the programmer.

 LANGUAGE TRANSLATOR

A language translator convert programming language or source code into the

language that computer processor understands. In other words, a language

translator converts a high-level language into a machine language. There are

three types of language translators: Assembler, Compiler and Interpreter.

Translator

Assembler Compiler Interpreter

Let us learn about all the three types of translators one by one:
 Assembler: An assembler is a program that converts an assembly

level language code (also called as mnemonic code) into machine

language code and provides necessary information for the loader to load

the program. In other words, an assembler allows the computer to

converts the assembly language instructions into machine code, i.e. 0s

and 1s.

 Compiler: A compiler takes and converts the entire program into

machine language before executing it. It converts the entire high-level

language program into machine language at once. Since it executes the

entire program at once hence there is less scope of correction, thus if

there is an error in the program it displays the errors for the whole

program together.

 Interpreter: An Interpreter is also a program that converts the entire

source program into machine language, but the only difference between

a compiler and an Interpreter is that an interpreter takes a single

statement or a single line of code at a time, whereas a compiler a

compiler takes the entire program in one go.


Let us quickly understand the difference between an Interpreter and a
Compiler:

Table 8.1 Differences between Interpreter and Compiler

Interpreter Compiler

An interpreter takes a single A compiler takes the entire


statement or a single line of code at program in one go.
a time.

It takes less amount of time to It takes more time to analyse the


analyse the source code, but as it source code, but the overall
takes single statement at a time and execution time is fast.
thus the overall execution time is
slow.

It continues translating the Error message is generated only


program until the first error is after scanning the whole program.
cited. This makes debugging easy. This makes debugging
comparatively hard.

An interpreter is best suited for a The compiler is best suited for the
software development production environment.
environment.

Languages, such as Python and Languages, such as C and C++ use


Ruby use interpreters. compilers.
 PYTHON

Python was conceived by Guido van Rossum in the late 1980s at Centrum
Wiskunde & Informatica (CWI) in the Netherlands as a successor to the ABC
programming language. He published the first version of Python code in 1991.

 FEATURES OF PYTHON
 Easy to learn and use: Since it uses a simplified syntax with an
emphasis on natural language, for much easier learning curve for
beginners.
 General-purpose programming language: The python language is so
flexible that it gives the developer the chance to try something new. It is
thus used in various fields like education, software and hardware
development etc.
 Cross-platform language: It means python can modify the apps made
in other languages also.
 Object-Oriented: Python has an object-oriented approach. This means
that the programs are designed using objects and classes that interact
with each other.
 Python is an interpreted language: An interpreted language is a
language that reads code in its raw form, and then executes it at a time
without being first compiled.
 Efficiency, Reliability and Speed: As it is a very simple language
therefore, can be used in nearly any kind of environment. Also, the
best thing about python is that it can be used in many varieties of
environments such as mobile applications, desktop applications, web
development, hardware programming etc.
 General-purpose programming language: The python language is so
flexible that it gives the developer the chance to try something new. It is
thus used in various fields like education, software and hardware
development etc.
 Dynamically Typed language: Here, dynamic implies that you do not
need to declare the type of variables in advances. For example, if you
write y=25, you need not to specify the variable y as an integer or stting.

 INSTALLING PYTHON

You can download and install python by following these steps:

1. Go to www.python.org

2. Click on Downloads, and then click on the latest version available for
Windows OS (Fig. 8.1).

Fig. 8.1 Downloading Python

3. Double-click on the setup file. The Setup wizard opens.

4. Click on the Install Now button (Fig. 8.2).


5. The Setup Progress window opens. Wait a few minutes and display setup
was successful. Here, the Progress bar shows the progression of Python
installation (Fig. 8.3). Once completed, click OK.

6. Next search for IDLE python, then click enter. Next, click the file button. You
will see a new file again.

7. Next, click the file button. You will see a new file again. Click the new file
button.

 PROGRAMMING IN PYTHON
There are basically two modes of programming in Python:

1. Interactive Mode: This is the line processing mode. It gives immediate


feedback for each statement while running previously fed statements in
active memory. This mode is a good way to play around and try
variations on syntax.
2. Script Mode: This is the batch processing mode. In this mode, the
Python program is written in a file. Python interpreter reads the file and
then executes it and provides the desired result.

INTEGRATED DEVELOPMENT AND LEARNING


ENVIRONMENT (IDLE)
To execute any Python program, the Python interpreter must be installed, for
installation of python steps are mentioned above.

An Integrated Development Environment (IDE) is a program dedicated to


software development. IDEs integrate various tools designed for software
development. The popular IDE for Python is IDLE (Integrated Development
and Learning Environment) which also gets installed by default when you
install Python. IDLE is GUI (Graphical User Interface) which includes both an
interactive mode and options to edit and run files. This is used to modify,
execute, browse, or debug Python programs from a single interface, which
makes the program execution easy.

 Working in Interactive Mode:


You can work in interactive mode in python by following these steps.

Step 1: Click on the Start button. s

Step 2: All apps > Python 3.8 > IDLE (Python 3.8 64-bit).

The Interactive mode appears which is also known as Python Shell.

 Components of Python Window


There are two components of Python IDLE window which are:

 Menu bar: It is composed of various menus, such as File, Edit, Run, and
so on with different options.
 Prompt: You will see a blinking cursor after the symbol (>>>). This is
called as Prompt or Prompt string. Prompt String (>>>) suggests that
interactive shell is now ready to take new commands. So, the prompt
on the screen is a sign that you are working in Interactive mode of
Python.
By typing the commands at the prompt you will get immediate results. For
example, type 3+3 and then press Enter. The result 6 will be displayed on the
screen. You can try for more values and immediate result will be displayed.

 Working in Script Mode:


The Interactive mode works by executing a single statement or a specific
block of code, and gives the output of the command that is entered. But this is
not what is always needed, in real-life as large programs are to be developed
and for that Script mode is needed.

 Creating a New File


You can create a new file by following these steps:

Step 1: Open the Python IDLE window.

Step 2: Click on the File menu. A drop-down menu will appear.

Step 3: Now, click on the New File option. A new editor window with a blank
file will appear on the screen.

Writing a Program
Type the program code in Python window.
NOTEPAD

Print (“I am learning programming”)

Typing Python program code

Saving Python Program

To save a typed program, follow these steps:

1. Right-click the Python window and select Save As to save your code.
2. The Save As dialog box will appear. Enter the filename.
3. Select Python files in Save as type drop-down box, if it is not selected, by
default.
4. Click on the save button to save the file.

Saving as dialog box


KEYBOARD SHORTCUTS

 Ctrl + N: To open a new file


 Ctrl + S: To save a program
 F5: To run a program
 Alt + F4: To close a program
 Ctrl + Q: To exit Python

Running a Python Program

Executing a program in python can be done by following these steps:

1. Click on the Run menu.


2. Click on the Run Module option. If there is no error, the program will
run successfully.

INPUT AND OUTPUT


Python provides two commonly used functions input() and output() for
input and output.
 The input() Function
The input() function in Python take input from the user. Given below is the
syntax of input() function:

input ([<prompt>])

Here, prompt is the string or message we wish to display on the screen.


Example:

Name = input (“Please type your name: “)

>>> Please type your name: “Shakshi”

 The print() Function


The print () function prints the specified message to the screen, or other
standard output device. It can also be used for printing the output of any
command. The general syntax of the print() function is as follows:

Print (<expression 1>, <expression 2>…)

For example:

>>> print ("Learning Python is easy") will display

Learning Python is easy

The print() function first converts a message or an object into a string before
displaying it on the screen. It can have multiple parameters. It supports
multiple escape sequences to format the output. In Python strings, the
backslash ‘\’ is a special character, also called the 'escape' character. It is used
in printing certain whitespace characters such as new line (\n), tab space (\t),
and carriage return (\r).
Note: Multiple arguments can be passed to the print () function. In such a
case, the arguments are separated by commas.

Let us create a program to use the input() and print() functions.

File Edit Format Run Options Window Help

name= input (“Please type your name: ”)

class= input (“Please enter your class: ”)

Print (name, “your class is”, class)

Use Input() function

File Edit Format Run Options Window Help

>>>

Please type your name: Rohan

Please enter your class: VI

Rohan your class is VI.

>>>

Use Print() function


VARIABLES IN PYTHON
A Python variable is a reserved memory location to store values. Once an
object is assigned to a variable, you can refer to the object by that name. But
the data itself is still contained within the object. You can store only one data
value at a time in a variable. When a new value is stored in a variable, the
previous value gets overwritten. As you know Python is a dynamically typed
language thus it identifies variable type automatically. Let us try to
understand this with the help of an example:

num= 2.3

_price= 10.4

TOTAL= round (_price * num, 2)

print (TOTAL)

Rules for Writing the Variable Names

In Python, there are certain rules which have to be followed for forming valid
variable names. The rules for valid identifier (or variable name) are given
below.

 Variable names are lower case and words are separated with
underscores (example: standard_deviation).
 Class names are capitalized camel case (example: Color Matrix).
 Identifiers that begin with one or more underscores have special
meaning.
 First character must be a letter or an underscore.
 After first character you may use letters, digits, or underscores.
 Variable names are case sensitive.
 Variable names should reflect its use.

Examples of some valid variables are Emp_code, maxvalue, bal_free, a24b,


_num, and so on.

Declaring and Initializing a Variable

Being a dynamically typed language Python allows the programmer to declare


and initialize at the same time in the following way:

m=30

n=40

print (“m=”, m)

print (“n=”, n)

For the above code the screen will display the following output:

m=30 and n=40 (only the bold is output)

You can assign multiple values to multiple variables in the same line like this:

name, class, roll number= “Manish”, ‘VII’, 24

print (“Name is”, name)

print (“Class is”, class)

print (“and Roll No. is”, roll number)


The above code will generate the following output, Name is Manish, Class is
VII and Roll No. is 24.

Fascinating Fact

The name Python was derived from the British comedy series
“Monty Python’s Flying Circus”.

DATA TYPES

Let us understand with an example. Suppose you and your family are going for
a trip. For this, you need to book tickets for everyone. Firstly, you need to fill
the reservation form, where you are supposed to fill some information, such
as date of journey, name, age, train number or flight number, and so on. All the
data that you enter are of a specific type, like your ‘can be of numeric or
alphanumeric type and ‘date of journey’ is a date type. name’ is a string type,
‘age’ is of numeric type, ‘train number’ or ‘flight number’

In Python, you should clearly specify the type of values will be stored in a
specific variable using data types.

Basic Data Types in Python


The data type means the type of data stored in a variable. As you know that
there are many types in which data is stored. For instance, a person’s address
is stored as alphanumeric data, age is stored in the form of numeric data.
Some of the basic data types in Python are discussed below:

 Numeric: This data type stores numerical values such as integers and
float values (decimal numbers).

Table 8.3 Numeric Data Types

Numeric Data Types Description

integer (int) It stores value in the range of -2147483648 to


2147483647.

Float The float type in Python represents the floating-


point number. Float is used to represent real
numbers and is written with a decimal point
dividing the integer and fractional parts. The
fractional part of a floating number can be O as well.
Examples for floating point numbers are 32.8,
54+e21, -55.6, -18.23e50 and so on.
 Boolean (bool): It is used to represent the truth value of an
expression i.e. it shows the logical values in the form of True and False.
In Boolean, 1 means True whereas 0means False.
 String (str): Strings are arrays of bytes representing Unicode
characters. A string of characters enclosed within single ('') or double ("
") quotes is represented in this data type. Examples of strings are:
“Hello, World!”, '545', 'yourage', 'hello', and so on.

FASCINATING FACT

If a variable is used without being assigned a value is not defined, and


gives error.

COMMENTS IN PYTHON

Comments in Python can be used to explain Python code. They can be used to
make the code readable i.e. it allows us to understand how a program works.
Comments are not a part of the program, but they do enhance the interactivity
of the program.

How to create a comment

#This is a comment

print(“I am learning Programming”)

Note: Comments can be placed at the end of a line, and Python will ignore the
rest of the line:
For example:

print (“I am learning Programming”) #This is a comment.

Python supports two types of comments: Single line comment and Multiline
comment.

 SINGLE LINE COMMENT

Python single-line comment start with the hashtag symbol (#) with no white
spaces and lasts till the end of the line.

For example:

Program: Output:
#printing a string I am learning programming
print(“I am learning programming”

 MULTILINE COMMENT

Python does not provide an option for multiline comments. However, there
are different ways through which we can write multiline comments. To add a
multiline comment, you could insert a # for each line.

Program: Outpuy:
#printing a string I am learning Python
#print five names Manish
print (“I am learning programming”) Neha
print (“Manish”) Sanskriti
print (“Neha”) Aman
Chirag
print (“Sanskriti”)
print (“Aman”)
print (“Chirag”)

Note: The text written inside triple quotes i.e. ( ‘‘‘ or ”””) is also considered as
comment. You can use triple quotes to write multiline comments. For
instance:

‘‘‘Demonstrating multiline comment using triple quote’’’

OR

“ “ “Demonstrating multiline comment using triple quote” ” ”

OPERATORS

Operators are used to perform operations on variable and values. Python


divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Logical operators
 Relational operators

 Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations. Arithmetic operations are defined in the table given
below:

Operator Name Description Example (a=8 Oputput


and b=4)
+ Addition Adds values on a+b 12
either side of
the operator.
__ Subtraction Subtracts right a-b 4
hand operand
from left hand
operand
* Multiplication Multiple values a*b 40
on either side
of the operator
/ Division Divides left a/b 2
hand operand
by the right
hand operand
% Modulus Divides left a%b 0
hand operand
by the right
hand operand
and returns
reminder
** Exponential Performs a**b 4096
exponential
(power)
calculation on
operands.
// Floor or Divides and a//b 2
integer cuts the
division fractional part
from the result

Program 1: To perform all the arithmetic operators.


Output of the above program is:

ASSIGNMENT OPERATOR

Assignment operators are used to assign values to variables:

Operator Name Description Example


= Assignment It assigns the a=8
value of operand
on the right side
to the left side
operand
+= Addition It adds right a+=4
assignment operand to the
left operand and
assign the result
to left operand.
a=a+4 is
equivalent to
a+=4
-= Subtraction It subtracts right a-=4
assignment operand from the
left operand and
assigns the result
to left operand.
a=a-4 is
equivalent to a-=4
*= Multiplication It multiplies right a*=4
assignment operand with the
left operand and
assigns the result
to left operand.
a*=4 is equivalent
to a=a*4
/= Division It divides left a/=4
assignment operand with
right operand and
assigns the result
to left operand.
a/=4 is equivalent
to a=a/4.
%= Remainder It takes modulus a%=4
assignment of two operands
and assigns the
result to left
operand. a%=4 is
equivalent to
a=a%4
//= Floor division It performs floor a//=4
assignment division on
operators and
assigns the value
to the left
operand. a//=4 is
equivalent to
a=a//4
**= Exponential It performs a**=4
assignment exponential
(power)
calculation on
operators and
assigns the value
to the left
operand. a**=4 is
equivalent to
a=a**4
Program 2: To perform all the assignment operators.

#examples of assignment operators


a=42
b=20
c=0

c=a+b
print (c)
c+=a
print (c)
c*=a
print (c)
c/=a
print (c)
c=4
c%=a
print (c)
c**= a

print (c)

c//=a

print (c)
Output:

62

104

4368

104

4398046511104

104715393121

>>>

LOGICAL OPERATORS
Logical operators are used to combine conditional statements:

Operator Description Example Output

AND Returns True if a<10 and a<20 TRUE


both statements
are true.

OR Returns True if a<10 or a<8 TRUE


one of the
statements is
true.

NOT Reverse the not(a<20 and FALSE


result, returns a<10)
False if the result
is true
Program 3: To perform all the assignment operators.

x=True

y=False

print (x and y)

print (x or y)

print (not x)

print (not y)

Output:

False

True

False

True

>>>

RELATIONAL OPERATOR
They are also called as comparison operator. They are used to compare two
operands and returns True or False as a result. Relational operators are
describes in the following table:
Operator Name Description Example Output
(a=4
and
b=3)
== Equal It checks a==b FALSE
whether the
value of two
operands are
equal or not.
If yes the
condition is
true else
false.
!= Not equal It checks if a!=b TRUE
the values of
two operands
are equal or
not. In this
case id the
values are
not equal
then
condition
becomes true
else false.
> Greater It checks if a>b TRUE
than the value of
the left
operand is
more than
the right
operand. If
yes, then
condition
becomes
true.
< Less than It checks if a<b FALSE
the value of
the left
operand is
less than the
right
operand. If
yes, then
condition
becomes
true.
>= Greater It checks if a>=b TRUE
than or the value of
equal to the left
operand is
more than or
equal to the
value of right
operand. If
yes, then
condition
becomes
true.
<= Less than It checks if a<=b FALSE
or equal to the value of
the left
operand is
less than or
equal to the
value of right
operand. If
yes, then
condition
becomes
true.
Program 3: To show all the relational operators.

a= 22

b=64

#check if a is greater than b

Print (a>b)

#check if a is less than b

Print (a<b)

#check if a is equal to b

Print (a==b)

#check if a is not equal to b

Print (a!=b)

#check if a is greater than or equal to b

Print (a>=b)

#check if a is less than or equal to b

Print (a<=b)

Output:

FALSE

TRUE

FALSE

TRUE

FALSE

TRUE
PRECEDENCE OF OPERATORS
The word “precedence” implies the order in which any work is to be executed
according to the conditions. Similarly, here also it determines the order in
which the operators are executed. In the table given below the precedence in
Python is listed, the highest precedence is at the top and the lowest at the
bottom.

Operator Name

() Parenthesis

** Exponent

*, /, %, // Multiplication, Division , Modulo,


Floor Division
+, - Addition, Subtraction

==, !=, >, <, >=, <= Comparison

=, +=, -=, /=, *=, /=, %=, **=, //= Assignment

And, or, not Logical

SOME MORE PROGRAMS

Program 1: Python Program to calculate the Area of a Triangle.

If a, b and c are three sides of a triangle. Then,

s = (a+b+c)/2

area = √(s(s-a)(s-b)(s-c))
# Python Program to find the area of triangle

a=5

b=6

c=7

# Uncomment below to take inputs from the user

# a = float(input('Enter first side: '))

# b = float(input('Enter second side: '))

# c = float(input('Enter third side: '))

# calculate the semi-perimeter

s = (a + b + c) / 2

# calculate the area

area = (s*(s-a)(s-b)(s-c)) ** 0.5

print ('The area of the triangle is %0.2f' %area)

Output:

The area of the triangle is 14.70


# Python Program to convert temperature in celsius to fahrenheit

fahrenheit = celsius * 1.8 + 32

# change this value for a different result

celsius = 37.5

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'


%(celsius,fahrenheit))

Output:

37.5 degree Celsius is equal to 99.5 degree Fahrenheit


LET’S RECALL

 Python is one of the High-level languages used for writing codes for
computer programs.
 Interactive Mode is the line processing mode in which a command is
typed once and get the result immediately.
 Script mode is used to write a lengthy program.
 In a program, values are stored in variables. However, keywords cannot
be used as variable names.
 The print () function is used to display the output of any command or
message.
 There are three types of translators: Assembler, Compiler, and
Interpreter.
 Comments allows us to understand how a program works.
 Operators are used to perform operations on variable and values.
 Python divides the operators in the following groups: Arithmetic
operators, Assignment operators, Logical operators, Relational
operators.
 Assignment operators are used to assign values to variables
 Logical operators are used to combine conditional statements
 Relational operators are used to compare two operands and returns
True or False as a result.
EXERCISES
B. Fill in the blanks.

1. ____________________is composed of various menus, such as File, Edit, Run, and


so on with different options.

2. IDLE stands for _________________________________________________.

3. By default, Python files are saved with _____________________.

4. ________________________ represent the type of data stored in variables.

5. Logical AND operator returns __________, if one value is false.

A. Tick (right) the correct answers.

1. The data type for 27.8 should be

a. Float b. str c. int d. None of these

2. Which of the following is an invalid variable?

a. EMP b. _emp c. 34emp d. 4emp65

3. Which of the following operation cannot be performed on a Python

string?

a. Deletion of Python String

b. Splitting of Python String

c. Adding elements in a Python String

d. Reassigning pf Python String

4. The statement using AND operator results true if _____________


a. Both operands are true

b. Both operands are false

c. Either of the operand is true

d. Either of the operand is false

5. What is the output of >>>print (400+200)

a. 200400 b. 400200 c. 600 d. 10200

C. Write T for True and F for False.

1. In Boolean, 0means True whereas 1means False._______________

2. Assignment operators are used to perform arithmetic operations between


operands _______________.

3. A variable can hold different types of values ________________

4. -18.23e50 is an integer numeric data type. _______________

5. In a program, keywords cannot be used as variable names. ______________

D. Answer the following questions.

1. What is Python?

__________________________________________________________________________________________

__________________________________________________________________________________________

__________________________________________________________________________________________
2. Write any three differences between an interpreter and a compiler.

__________________________________________________________________________________________

__________________________________________________________________________________________

__________________________________________________________________________________________

3. Define a variable. What are the rules of writing a variable.

__________________________________________________________________________________________

__________________________________________________________________________________________

__________________________________________________________________________________________

4. What are two modes of Python.

__________________________________________________________________________________________

__________________________________________________________________________________________

__________________________________________________________________________________________

5. What do you understand by operation precedence?

__________________________________________________________________________________________

__________________________________________________________________________________________

__________________________________________________________________________________________
LAB ACTIVITY

A. What will be the output of the following programs?

1. a=9

b=4

print (a//b)

2. x=20

X-=4

print(x)

3. var1=1

var2=2

var3=3

print(var1+var2+var3)

4. a=8

b=22

print(a or b)

print(a>4)
B. Write the output for the following program for temperature value
in fahrenheit as 52 degree celsius.

# Python Program to convert temperature in celsius to fahrenheit

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'

%(celsius,fahrenheit))

C. Write the programs to:

1. Subtract two numbers.

2. Calculate the area of a parallelogram.

3. Input five names from the user.

4. Convert days into minutes.

Group Discussion
Conduct group discussion on the following topics:
 Why Python is called an interpreted language?
 Variables and data types

Teacher's Notes
 Discuss about Python programming modes with the students.
 Show how to write simple print statement and arithmetic expressions.
Also, show the use of separators in print statements.

You might also like