G6 - Unit 2 - Coding
G6 - Unit 2 - Coding
Learning Objectives
Learners will learn to:
develop text-based programs that use input and output
including +, −, *, /
Vocabulary
source code
python shell
variable
data type
Let Us Start
Hey! I am aware that Python Not at all! Python is known for its
programming can be used to simplicity and readability. It has a
develop a wide range of simple syntax, which makes it easy to
applications. Is it difficult to get understand and write code. It's a high‐
started with Python level programming language, meaning
programming? you don't have to worry about low‐
level details like memory management.
This makes it great for beginners and
experienced programmers alike.
1
Let Us Explore
We know that a computer programmer creates code to develop specialised and
Make a list of some programming languages that you have used earlier or that you
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
Get, Set, Go
2.1 What is Python?
Python is a popular high-level programming language. It was created by Guido van
Rossum and released in 1991. It was mainly developed to emphasise code readability
and its syntax allows programmers to express concepts in fewer lines of code. It is
used for:
software development
mathematics
system scripting
Pi, etc.
2
Python has a syntax that allows developers to write programs with fewer lines
which means that its source code is freely available for modification by
anybody.
soon as it is written.
such as Notepad or Notepad++. However, with a text editor, you will need to use
other tools to run or execute the program. To avoid this, you can use an Integrated
An IDE enables you to create, test and execute programs within the same
environment. There are many different types of IDEs. One example of an IDE for
comes with many useful features, such as syntax highlighting and autocomplete,
making writing and reading Python code easier. Python IDLE also includes a
debugger, which allows you to execute your code line by line to find and fix errors.
Tech Tickler
PyCharm, Vim, and Emacs are examples of a few other IDEs, that you can use
for creating, editing, and executing Python programs.
individual instructions of Python. You can also use it to create, edit and execute
3
Steps to download and install Python IDLE
1. To download the setup file, open the web page.
will download the setup file to the local drive of your computer.
5. Check both checkboxes at the bottom of the dialogue box. Then, click the
Install Now option. The process of installing IDLE will start in the system.
4
Using the Python
To open Python IDLE, click the icon from the Start menu. When you first start
In the Python programming language, there are two ways in which we can run our
code:
Interactive mode
Script mode
Interactive mode
In the screenshot, the blinking line is the cursor. This is where you type Python
commands and get the command output immediately in the next line. This is called
The interactive mode is useful for typing a few lines of instructions or commands.
Script mode
In the script mode, a Python program can be written in a file. This file can then be
saved and executed using the command prompt. We can view the code at any time
5
by opening the file and editing becomes quite easy as we can open and view the
Script mode is very suitable for writing long pieces of code. It is much preferred over
the interactive mode by experts in the program. The file made in the script mode is,
by default, saved in the Python installation folder and the extension to save a Python
file is “.py”.
menu. Type the program and save the file with an extension “.py”. IDLE uses different
colour codes to show different types of text: the print command is in purple, the
Step 2: Click the New File option from the File menu of the shell.
Step 3: A new window opens up on the screen. You can type the commands of a
program here.
6
The given program contains instructions to print text.
Step 2: Select the folder where you want to save the file.
Step 3: Type a file name. Make sure the file type is selected as Python files. The file
7
Steps to execute a Python program
Step 1: Once you have saved a program, select the Run Module option from the Run
Step 2: The script is executed and the output of the script is displayed in the Python
Shell.
display text or numbers. The value to be displayed is given inside the round brackets.
Text in Python is also called string and refers to a sequence of letters or characters. It
8
Comments
A Python command can include comments to help someone else understand your
code. The Python interpreter ignores them. Comments start with a ‘#’. Comments can
be used to
Activity 1
Perform the following tasks.
Task 1: Open Python IDLE on your computer.
Task 2: Write a command to display the following text in the interactive mode.
Task 3: Use the script mode to create and save a program for Task 2.
Task 3: Remove the quotes from the text to be printed and run the program once
2.2 Variables
A variable is a named storage area in the computer memory used to store a value.
You can change the value of data stored in this area, but the variable's name will
remain the same. Every variable must be named. The name given should be
meaningful so that you can remember what the variable is used for in the program.
For example, a variable called score can be used to store the score in a game and a
variable called answer can be used to store the answer selected by the user.
9
Rules for creating Python variables
A variable name can contain an alphabet, numeric digit and underscores (A-Z,
0-9 or _).
same variable in uppercase. For example, score, Score and SCORE are three
different variables.
that the variable can store and what kind of operations can be performed on that
particular data. Python decides a variable’s data type by the value assigned to it. The
Floating Point It contains a number with a decimal point. 56.8, -45.0, 23.6671,
‘float’ It can be positive, negative or zero. 0.314
calculations.
10
Assigning values to variables
To define a variable, you must give it a value using the ‘=’ sign. This is called
For example,
a, b, c= “Rose”, “Marigold”, “Lotus” will store ‘Rose’ in the ‘a’ variable, ‘Marigold’
input() command. The following command assigns the input typed by the user into
the variable x.
x= input()
You can also add a message informing the user what they should type in. This
11
Output
Activity 2
Write a line of code that asks the user to input their age. Store this value in a variable
2.4 Operators
Often, you will need to perform calculations in a program. Python offers a wide range
sum
difference
the product
quotient
12
Example 1: A program that uses arithmetic operators on two variables and displays
the results.
Program
Output
Example 2: A program that takes input from the user and multiplies the numbers
Program
13
Output
Activity 3
Create an Addition program in Python that takes two numbers entered by the user. It
2.5 Errors
A Python command will not execute if there is an error in its syntax; instead, it will
display an error message with a description of the error, as shown in the following
screen. An error occurs when you do not follow the rules for typing Python
commands.
For example, a command defined in a small case cannot be typed in the upper case.
‘P’ in
uppercase
Error
In the example shown, the ‘print’ command is typed with the letter ‘P’ in uppercase,
which results in an error. Other errors could also be a missing bracket or a spelling
mistake. When there is an error, you need to correct the same and execute the script
once again.
In the following program, the check module shows the line with the error. The
14
After using the Check Module command, you can open the file using the Open
option from the File menu and make changes to the file. Save the changes using the
Tech Tickler
You can also use shortcut keys for the various actions as given below.
• Create a new program: Ctrl+N
• Open a saved program: Ctrl+O
• Save a program: Ctrl+S
• Save a program with another name: Ctrl+Shift+S
• Run a program: F5
Unit Review
1. Python is a popular high-level programming language. It was created by
3. An IDE enables you to create, test and execute programs within the same
environment.
4. The mode where you type Python commands and get the command output
immediately in the following line is called the interactive mode of the shell.
15
5. In the script mode, a Python program can be written in a file. This file can then
6. To save the program in Python, use the file with an extension “.py”.
7. Select the Run Module option from the Run menu to execute the program.
your code. The Python interpreter ignores them. Comments start with a ‘#’.
10. A variable is a named storage area in the computer memory used to store a
value.
11. The data type determines the type of data that the variable can store and
12. Data types used in Python are integers, float, character, string and boolean.
13. To define a variable, you must give it a value using the ‘=’ sign. This is called
operations on numbers.
16. An error occurs in Python when you do not follow the rules for typing Python
commands.
17. To check for the errors, click the Run menu and select the Check Module
option.
16
b. What will an Integrated Development Environment (IDE) enable you to do?
i. Create a program
ii. Execute a program
iii. Both a. and b.
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
Programming.
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
17
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
d. Identify the errors in the following program. Rewrite the program correctly.
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
______________________________________________________________________________
18
Lab Activity
Q1. Create a Python program using the following instructions.
a. Accept the student’s name, exam name and the percentage marks obtained,
b. Calculate and display the total marks and percentage scored by the
student
Resources
https://www.javatpoint.com/python-tutorial
Reference Material
https://www.programiz.com/python-programming/first-program
19