0% found this document useful (0 votes)
2 views55 pages

Introduction to Python Programming

This document serves as an introduction to Python programming for Grade 8 students, covering its advantages, applications, and key concepts such as variables, data types, and control statements. It explains the use of Python's print function, assignment operator, and operators for arithmetic and logical operations. The document also includes assignments and examples to reinforce learning about Python syntax and programming structures.

Uploaded by

shreedesh08
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)
2 views55 pages

Introduction to Python Programming

This document serves as an introduction to Python programming for Grade 8 students, covering its advantages, applications, and key concepts such as variables, data types, and control statements. It explains the use of Python's print function, assignment operator, and operators for arithmetic and logical operations. The document also includes assignments and examples to reinforce learning about Python syntax and programming structures.

Uploaded by

shreedesh08
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/ 55

INTRODUCTION TO

Grade 8
INTRODUCTION

Python is a high-level, interpreted, and general-purpose dynamic


programming language that focuses on code readability.
It has fewer steps when compared to Java and C.
It was founded in 1991 by developer Guido Van Rossum.
Python ranks among the most popular and fastest-growing languages in the
world.
Python is a powerful, flexible, and easy-to-use language.
In addition, the community is very active there. It is used in many
organizations as it supports multiple programming paradigms. It also performs
automatic memory management.
ADVANTAGES

1.Open source, versatile, easy to read, learn and write general purpose
programming language
2.User-friendly data structures
3.High-level language with simple syntax
4.Object-oriented language
5. Platform independent, Portable across Operating Systems and Interactive
6.It’s an Interpreted language. [Interpreted language is a programming
language in which programs are 'indirectly' executed ("interpreted") by an
interpreter program. This can be contrasted with a compiled language which
is converted into machine code and then 'directly' executed by the host CPU.]
7.Ideal for prototypes – provide more functionality with less coding
APPLICATIONS

 Build a website
 Develop games
 Program robots
 Perform scientific computations
 Develop AI applications
 Enterprise and Business applications
ORGANIZATIONS USING PYTHON

1.Google(Components of Google spider and Search Engine)


2.Yahoo(Maps)
3.YouTube
4.Mozilla
5.Dropbox
6.Microsoft
7.Cisco
8.Spotify
9.Quora
PLATFORM FOR PYTHON PROGRAMMING
 Colab (short for Colaboratory) is Google’s free platform which enables
users to code in Python. It is a cloud service, provided by Google. This
platform allows us to train the Machine Learning models directly in the cloud
and all for free.
 Some of Google Colab’s advantages include quick installation and real-time
sharing of Notebooks between users.
 To get started, sign in to your Google Account, and then go to
“https://colab.research.google.com” and click on “New Notebook”. Programs
wiil be saved in your drive.
USING PRINT() FUNCTION

 It is used to display the output of any


command on the screen. It can also be used
to print the specify messages.
 Python is a case sensitive programming
language. It means that Python
differentiates between capital and small
letters. For example, Print and print are
two different things for Python, where
print is a valid command while Print is
just a word, not a command.
 We can also pass more than one
argument to the print() function. In
such case, the arguments are separated
by commas.
SEPARATORS WITH PRINT()

 ‘,’ comma- to point the next value


after a space
 ‘\n’ newline- to print the value in
the next line
 ‘\t’ tab- to print the next value after
a tab space (1 tab= 4 spaces)
PYTHON CALCULATOR

 To evaluate an arithmetic
expression, it is not necessary to
use print() function. We can type
th expression, and the
interpreter (which acts as a
simple calculator) automatically
evaluates and shows the results.
PYTHON KEYWORDS AND
IDENTIFIERS
In this part of the lesson you will learn about keywords (reserved words in Python) and identifiers
(names given to variables, functions, etc.).
PYTHON KEYWORDS

 Keywords are the reserved words in Python


used by Python interpreter to recognize the
structure of the program.

 During coding keywords cannot be used as


variable name, function name or any other
identifier. They are used to define the syntax
and structure of the Python language.

 In Python, keywords are case sensitive. All the


keywords except True, False and None are in
lowercase and they must be written as they
are.
PYTHON IDENTIFIERS

 An identifier is a name given to entities like class, functions, variables, etc. It


helps to differentiate one entity from another.

Rules for
writing
identifiers
EXAMPLE
EXAMPLE
VARIABLES IN PYTHON
You need to store for your future also, what will you require? Well you need a contain er to store the food.
Similarly, when you are working with the values in Python, you require some storage to hold values for later
use. Such storage locations are known as variables.
DEFINITION OF VARIABLE

 Variables are referred to "envelop" or "buckets" where information can be


maintained and referenced. Like any other programming language
Python also uses a variable to store the information.
 Variables are reserved memory locations to store values. A variable can
store only one data value at a time. When a new value is stored in a
variable, its previous value gets overwritten.
 In python everything is an object and variables are just names given to
identify these objects.
THE ASSIGNMENT OPERATOR (=)
 Values are assigned to variables using assignment operator. For example, the statement x=25
assigns the value 25 to the variable x.
 Similarly –
a=10 the value 10 is stored in the variable a;
b=20 the value 20 is stored in the variable b;
c=a*b the product of a and b, i.e. 10*20=200 is stored in the variable c
RULES TO WRITE THE VARIABLE NAMES

• The first character of the variable must be an alphabet or underscore ( _ ).


• All the characters except the first character may be an alphabet of lower-
case(a-z), upper-case (A-Z), underscore, or digit (0-9).
• Variable name must not contain any white-space, or special character (!, @,
#, %, ^, &, *).
• Variable name must not be similar to any keyword defined in the language.
• A variable name can be of any length.
• Variable names are case sensitive; for example, myname, and MyName is
not the same.
ASSIGNMENT-1

Variable names Valid or invalid? Reason ?

Employee code

10code

A.B.C LTD.

else

Student_blood_grp
ASSIGNMENT-2: EXECUTE THE FOLLOWING CODE AND MENTION THE OUTPUT

Task Input Output

Assigning a value to a variable website="abc.com"


print(website)

Changing value of a variable Website = "abc.com“


print(Website)
Website = "xyz.com"
print(Website)

Assigning different values to a,b,c = 1,2,"john“


different variables
print(a)
print(b)
print(c)

Assigning same value to different a=b=c=1


variable
print(a)

print(b)

print(c)
DATA TYPES-LET’S UNDERSTAND THE
CONCEPT
 Suppose, you have planned a holiday trip during vacations, and you have to book
tickets for your family members. The first action that you need to take is to fill a
form to get the reservation done, where you need to give your required details
like your name, date of journey, name , age etc. Here every piece of data that you
enter is of a specific type, as your name is a String type, age is a numeric type and
date of journey is of date type. Similarly, in Python, one must specify in a
program, what kind of values will be stored in a specific variable.
 While writing a Python program, you need to work on different types of data. F;or
example, if you have to enter the height of a person, you will be required to use
decimal value, but if you have to write the address of the person, then you need to
use a string or a collection of alphanumeric characters.
DATA TYPES -DEFINITION

 The type of data value that can be stored in an identifier, such as a variable, is
known as its data type. The data type of a value or variable is an attribute that tells
what kind of data a variable can have.
 Data types help in classifying different types of data values used in a program.
 Python has standard data types that are use to define operations performed on
them.
MAIN DATA TYPES USED IN PYTHON
Sr. no. Data types Explanation
1. Integer (int) Represents integral /whole numbers (positive or negative)

There are 3 types of integer


Plain integers (int): stores value in range of -2147483648 to 2147483648
Long Integers(long int): supports the value that lie beyond the range
of plain integers
Boolean (bool): logical values in form of True or False. In Boolean, 0
represents False and 1 represents True

2. Float (float) Represents floating points value; numbers with fractional part. The fractional
part of a floating point number may be 0 as well. Ex: 3.14, -48.6, 18.0 etc.
3. String (str) Represents strings of characters enclosed within quotation marks (‘ ‘ or “ “)
Example- ‘Hello’, “Employee_code”,’218’, etc.
TYPE() FUNCTION

 If you want to know what type of data you are working with, use the type()
function. It is use to return the data type of a value.
INPUT() FUNCTION

 It is used to accept the


value for a variable
from the user.
SWAPPING TWO NUMBERS
AREA AND PERIMETER OF A SQUARE
ASSIGNMENT 3-APPLICATION BASED
QUESTIONS
 Kritika’s teacher has given her an assignment to display the names of the fruits
separated with a tab space in Python. Which separator she should use with the
print() function?
 Write the code for the following program:
1. Write a program to find the product of two numbers x and y, where x=150 and
y=200
2. It takes 3 hours to drive a distance of 192 km on a motor way. Create a program
to calculate the average speed in km/h.
3. To calculate and print the circumference and area of a circle.
4. To input the roll no, name and marks in five subjects of a student. Calculate the
total and percentage marks of the student.
OPERATORS IN PYTHON
In the previous part of the lesson you have learnt how to declare and initialize variables. Int this part, you will learn how
to use operators to perform arithmetic and logical operations in programming.
WHAT ARE OPERATORS?

 Operators are special symbols in Python that


carry out arithmetic or logical computation. The Operators
value that the operator operates on is called the
operand.
 Operators are special symbols which represent
computation. They are applied on operand(s), Sum= a + b
which can be values or variables. Same
operators can behave differently on different
data types. Operators when applied on
operands form an expression. Operators are
Operands
categorized as Arithmetic, Relational, Logical
and Assignment.
to divide the numbers and give an output in decimal form

Integer division- to divide the numbers and give an output in


integer form

Remainder- when one value is divided by other

Exponential-to calculate the power of numbers


STRING OPERATORS

Also known as Replication Operator


TO FIND THE AREA AND CIRCUMFERENCE OF A
CIRCLE WHOSE RADIUS TO BE ENTERED BY THE
USER
EXAMPLE OF STRING OPERATORS

 Program to print the string entered by  Program to print “welcome” 5 times


the user 5 times
ASSIGNMENT 4

 Find the result of (75+85+65)/3 i.e. the average of three marks


 Find the result of 22/7 * 5 * 5 i.e. the area of circle having radius as 5
 Find the result of "RAVI"+"Kant"
 Find the result of "###" * 3
 Predict the output of the following statements:
print(52*7)
print(“52*7”)
print(“52*7=“, 52*7)
print(“52”*7)
Print(“52”==52)
print(“52+7”)
COMMENTS IN PYTHON
WHAT IS A COMMENT?

 Comments are the statements that are added to a program wih the purpose of
making the code easier to understand.
 Compilers and interpreters generally ignore comments during the execution of
the program.
 Comments are optional, they are needed from the user’s perspective.
TYPES OF COMMENT

 Single line- these are created by


beginning a line with the hash(#)
character and are automatically
terminated at the end of the
line.
 Multiline – sometimes the
program may need to explain
things in more details, which
may exceed one line. Such
comments are called multiline
comments. We can provide
multiline comment by adding a
delimiter (“ “ “) at the beginning
and the end of the comment.
Introduction to Python-
Part 2 Grade 8
Conditional statement
Introduction

• In a computer program, statements are generally executed in a


sequential manner. However, at times the user may need to
change this order of execution by repeating or skipping the
execution of a few statements , subject to given condition. In such
a situation, the flow of execution is altered by the use of control
statement.
• Conditional statement is a control statement which checks the
condition and execute the statements accordingly.
• Let us understand this concept with a simple example. Suppose,
you want to withdraw money from the ATM. You will be allowed to
withdraw money only if you enter the correct PIN number.
Condition Plan of action
If correct PIN is entered You can withdraw the money
If incorrect PIN is entered You cannot withdraw the money

Every decision involves a choice between the two


alternatives ‘yes’ and ‘no’ result. If a conditional
statement is true then one set of statements is
executed, otherwise the other set of statements
is executed.
If statement
• This statement is used to evaluate only one condition. It performs a course of action if the condition evaluates to true, otherwise it
skips the statements if the condition evaluates to false.
• For example, your parents allow you to go out for playing only if you complete your homework.
• Syntax:
if<condition>:
Statement 1
Statement 2
……………………

Note: after the if condition there is a colon (:) and the condition body starts with an indentation of tab space. It is mandatory in Python
to indent the statements in the condition body else it will display an error.

 In Python, the body of the if statement is indicated by the indentation.

 Leading white space (spaces and taps) at the beginning of each statement, which is used to determine the group of statement, is
known as “indentation”.
example

What will happen if you enter a number grater than 200? No output will be displayed because no
statement is given to be followed if the given condition is false.
If-else statement
• The if-else control structure is used when either of the two different actions is to be performed depending upon
the result of the conditional expression.

• It contains two blocks of statements. In case the conditional expression evaluates to true, the statements in the
‘if’ block are executed, and if the result is false, then the statement in the ‘else’ block get executed.

• For example, you can go out to play if it doen’t rain else you have to play indoor games.

• Syntax:

if<condition>:

Statement 1

else:

Statement 2
example
Assignment 5

• You have installed an application in your phone. The security feature of


the application allows you to enter a Pin number of 4digits only. Create a
program to check whether the Pin number entered by the user is a 4-
digit number or not.
• Create a program to input the runs scored by a batsman in a match.
Based on the scored runs by the batsman, the program should display a
message whether a batsman has scored a century or not.
• Create a program in Python to enter a number and check if the number
is a positive or negative.
• Create a program in Python to check whether the person is eligible for
driving licence. [note: to get the driving licence a person must be 18 yrs
of age or above]
Example:

Write a program to check the grade of a student using if-elif statement. The system should ask the
marks from the student. As soon as he/she enters the marks, the system should display grade based on
marks. Program:

Marks Grade
name=input("Enter your name:-")
Marks greater than equal to A marks=float(input("Enter your marks:-"))
75
Marks greater than equal to B if marks>=75:
60
Marks less than 60 C print("Your grade is 'A'.")
elif marks>=60:
print("Your grade is 'B'.")
else:
print("Your grade is 'C'.")
print("Congratulations for your grade! ")
If-elif-else statement
• Sometimes we need to work with multiple conditions. In this case, only using if-else construct doesn't
serve the purpose. The if..elif..else statements provide a compact way to perform multiple tests on a
condition.

• For example, when you visit a bank, you go to the counter according to the service you want to avail. If
you want to deposit cash, you go to counter 1, if want to enquire about the cheque, you go to counter 2
etc.

• Syntax:
if<condition 1>:
Statement 1
elif<condition 2>:
Statement2
else:
Statement 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy