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

Python_Basics_notes

The document provides an introduction to Python programming for Grade 8 students, covering basic concepts such as programs, programming languages, source code, and machine code. It explains how to start coding in Python using both interactive and script modes, along with syntax, comments, data types, operators, and conditional statements. Additionally, it includes practical activities and examples to reinforce learning about loops and user input in Python.

Uploaded by

krishivagarwal29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python_Basics_notes

The document provides an introduction to Python programming for Grade 8 students, covering basic concepts such as programs, programming languages, source code, and machine code. It explains how to start coding in Python using both interactive and script modes, along with syntax, comments, data types, operators, and conditional statements. Additionally, it includes practical activities and examples to reinforce learning about loops and user input in Python.

Uploaded by

krishivagarwal29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Basics

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:

The language used to specify this set of instructions to the computer


is called a programming language.

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.

You can download Python from https://www.python.org/downloads.


Working with Python: You can work in Python in two ways- -
● Interactive mode
● Script mode

How to start Coding in Python (INTERACTIVE MODE)

When you install Python, an IDE named IDLE is also installed.


You can use it to run Python on your computer.
When you open IDLE, an interactive Python Shell is opened.

Opening IDLE : (Windows 7 )


To open IDLE (Integrated Development Environment) follow these
steps:
Click the Start button.
Click All Programs.
Select Python.
Click IDLE.

The IDLE screen opens- called shell window


IDLE is a text editor which helps you get your code (program) right.
It is also known as the Python Interactive Mode.
The code that you want to execute is typed after the entry prompt
(>>>).
To run/execute a program/code in Interactive mode, press the Enter
key.

Example 1: Program to print Hello,

Example 2: Program to print 3+4


Congratulations! You just wrote your programs in Python.

As you can see, this was a pretty easy task.


This is the beauty of the Python programming language.
How to start Coding in Python (SCRIPT MODE) To open script mode,
follow these steps:
Open Python in interactive mode.
Click the File menu
Click the New File or New Window option.(as per version)

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

Syntax is the structure of an instruction in a computer language. It is


the grammar of a programming language. When the computer
executes the program and finds a mistake(s), it shows an error
message. Such a mistake in the instruction or code is known as syntax
error. The errors are also known as bugs.
See the example given below.

Comments in a Python program

Comments are important for adding essential information about


program statements.
The symbol # (hash) is used to put comments for the line of code.
Python ignores the text after the # symbol.
Assignments for Practice Write Python programs to print the following.

Add the appropriate Comment lines to each program.

1. My name is …..(type your name )


2. I study in Class V
3. My hobbies are .….( type your hobbies)
4. My School’s name is…..(type your School name)
5. My favourite subjects are…..(type your favourite subjects)
6. 10+2-5
7. 8*2-10
8. 4/2+12
9. 3*3*3
10. 2*2*2 ********

Python data types


Math operators in Python
Comparison operators in Python

In programming languages a double equal to ( ==) sign is used to


compare two values
and single equal to (=) sign is used to assign values.

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

Python language provides numerous in-built functions that can be used


at the Python prompt.

Functions like input( ) and print( ) are widely used for input and
output operations respectively.

You have learnt the print ( ) function,


Now let’s learn how to use the input () function.
input () gives flexibility to take input from the user.

Try this code. Here x is a variable store the value entered by the user.

print('Enter your name:')


x = input()
print('Hello, ' + x)

Practical activities

1. Create a variable called ‘name1’ and add ‘Singapore’ , name2


and add ‘International’
Name3 and add ‘School ’ and print all the variables together in line.

2. Create a variable called num and Collect a decimal value to num.


Display the double of the value

3. Accept temperature in celsius and convert to fahrenheit using


this formula
fahrenheit = (celsius * 9/5) + 32
Conditional statements in Python

Like other programming languages,conditional statements check for


the condition and accordingly evaluate the result.

Python provides the following types of decision making statements.


● if statement
● if ….else statement
● elif statement

If Statement The if statement contains a logical expression.


By using this expression ,the data is compared and a decision is made
based on the result of the comparisons.

Syntax:

if condition :

statement(s)

If the condition evaluates to TRUE,the block of statement(s) inside the


if statement is executed.
If condition evaluates to FALSE, the first set of code after the end of
the if statement(s) is executed.

The indentation implies that its execution is dependent on the


condition.
There is no limit on the number of statements that can appear as a
block under the if statement.
Practical Activity:

Write a program to check if the number is positive and print the


message.

Ask the user for input a number.


# If the number is positive, print a message

Program

num=float(input(“Enter a number:”))
if num > 0 :
print(“Positive number”)

Output

If …..else statement

An else statement can be combined with if statement that will execute


if the condition resolves FALSE value.

Syntax:

if condition: statement(s)

else: statement(s)

Practical Activity:
# Program to check if number is positive or negative.

num = float(input("Enter a number:"))


if num > 0:
print("Positive number")

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.

num = float(input("Enter a number:"))


if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative Number")

Logical Operators

There are three logical operators supported by Python.


These operators (and, or, not) are to be written in lower case only.

The logical operator Evaluates to either True or False based on the


logical operands on either side.
Practical Activities

1. In a Movie Rating system, users rate a movie on a scale of 1 to 5. Write Python


code to categorize the movie based on the following rules:
● If the rating is 4 or 5, label it as "Excellent".
● If the rating is 3, label it as "Good".
● If the rating is 1 or 2, label it as "Poor".

Input:

● Enter the movie rating (R).

Output:

● Display the category of the movie.

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:

● Enter the number of pizzas (N).


● Enter the cost per pizza (C).

Output:

● Display the total cost of the order (T)

File name: Question8_YourName.py

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.

Python supports two types of loops

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

Complete these Programs

1. Write a program to display first 10 natural number.


2. Write a program to find sum of odd numbers between 10 and 30
3. Write a program to display table of input number.
4. Write a program to input start value, stop value and step value and
display the output (Assuming start<stop)

While loop

With the while loop we can execute a set of statements as long as


a condition is true.

Example
Print i as long as i is less than 6:

i=1
while i < 6:
print(i)
i += 1

You might also like