CHAPTER 8 INTRODUCTION TO PROGRAMMING-print
CHAPTER 8 INTRODUCTION TO PROGRAMMING-print
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
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 Languages
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:
Assembly Language:
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.
LANGUAGE TRANSLATOR
Translator
Let us learn about all the three types of translators one by one:
Assembler: An assembler is a program that converts an assembly
language code and provides necessary information for the loader to load
and 1s.
there is an error in the program it displays the errors for the whole
program together.
source program into machine language, but the only difference between
Interpreter Compiler
An interpreter is best suited for a The compiler is best suited for the
software development production environment.
environment.
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
1. Go to www.python.org
2. Click on Downloads, and then click on the latest version available for
Windows OS (Fig. 8.1).
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:
Step 2: All apps > Python 3.8 > IDLE (Python 3.8 64-bit).
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.
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
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.
input ([<prompt>])
For example:
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.
>>>
>>>
num= 2.3
_price= 10.4
print (TOTAL)
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.
m=30
n=40
print (“m=”, m)
print (“n=”, n)
For the above code the screen will display the following output:
You can assign multiple values to multiple variables in the same line like this:
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.
Numeric: This data type stores numerical values such as integers and
float values (decimal numbers).
FASCINATING FACT
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.
#This is a comment
Note: Comments can be placed at the end of a line, and Python will ignore the
rest of the line:
For example:
Python supports two types of comments: Single line comment and Multiline
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:
OR
OPERATORS
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:
ASSIGNMENT OPERATOR
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:
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
Print (a>b)
Print (a<b)
#check if a is equal to b
Print (a==b)
Print (a!=b)
Print (a>=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
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
s = (a + b + c) / 2
Output:
celsius = 37.5
# calculate fahrenheit
Output:
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.
string?
1. What is Python?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
2. Write any three differences between an interpreter and a compiler.
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
LAB ACTIVITY
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.
# calculate fahrenheit
%(celsius,fahrenheit))
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.