Python_Basics_notes
Python_Basics_notes
Grade -8
ICT
Program:
An ordered set of instructions to be executed by a computer to carry
out a specific task is called a program.
Programming Languages:
Source Code:
● Source code is like a set of instructions written in a programming
language (like Python or Java) that humans can understand.
● It's similar to writing down steps to complete a task.
Machine Code:
● Machine code is the language that computers can understand
directly. It consists of binary code, which is a series of 0s and 1s.
● Think of it as the final set of instructions that the computer can
follow.
Compiling:
● Compiling is like turning the human-readable instructions
(source code) into computer-readable instructions (machine
code).
● It's the process of translating the steps you wrote into a
language the computer can understand.
Running:
● Running is when you ask the computer to actually perform the
instructions you wrote in your source code.
● It's like pressing the play button to see the computer follow the
steps.
Python is regarded as the simple and easy to learn programming
language because of the following reasons.
● Easy to learn and code.
● It has a simple syntax and readability.
● Python code is 3 to 4 times shorter than Java and 8 to 10 times
shorter than C++.
● It can run on various operating systems and hardware
platforms.
● Python is used for testing microchips, building video games, web
applications and a lot more. Python is also helpful in web
development.
● Many popular web services and applications are built using
Python.
● It has been used by organizations such as NASA, YouTube and
Instagram. It is also used for writing Google Apps.
A new blank window appears to type codes and save it for later use.
Type the program in the Window as shown below
The program in script mode will not run without saving the program.
Saving the program To Save the program click the File menu and click
the Save As option.
Type the file name in the Save As dialog box.
The file is saved with .py as a file extension.
Running a python program To run the program, click the Run menu
and then click the Run Module.
The output of the program appears on shell.
Syntax and Syntax errors
Practical Activity:
Compare two values and print the result
x=20
y=15
print( “x>y is” , x>y)
print( “x<y is” , x<y)
Inputs in Python
Functions like input( ) and print( ) are widely used for input and
output operations respectively.
Try this code. Here x is a variable store the value entered by the user.
Practical activities
Syntax:
if condition :
statement(s)
Program
num=float(input(“Enter a number:”))
if num > 0 :
print(“Positive number”)
Output
If …..else statement
Syntax:
if condition: statement(s)
else: statement(s)
Practical Activity:
# Program to check if number is positive or negative.
else:
print("Negative Number")
If…elif…else Statement:
The elif statement allows you to check multiple conditions for True. It
executes a block of code as soon as one of the conditions evaluates to
true.
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
Else:
statement(s)
Practical Activity:
Write a program to check if the number is zero, positive or negative.
Print appropriate message. And ask the user to input a number. #
Program to check if number is positive or negative or zero.
Logical Operators
Input:
Output:
2. Write a Python program for a Pizza Order Calculator. Users input the number of
pizzas they want to order and the cost per pizza. The program calculates and
displays the total cost of the order.
Input:
Output:
Loops in Python
Basics of for Loop
Sometime in programming language you need to execute a block of
code repeatedly,
this repetition of the statement is controlled by iterative statements in
python.
1. for
2. While
for loop :
The for loop is used when you are sure about how many times a loop
body will be executed. It is known as a definite loop.
Syntax
for<variable> in range(initial value,final value,step value):
Examples
for i in range(1,5)
print(i)
output
1
2
3
4
Remember the value displayed would be from 1 to 4
for i in range(1,10,2)
print i
output
1
3
5
7
9
While loop
Example
Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1