0% found this document useful (0 votes)
13 views

Grade 9 - Basics of Python

Uploaded by

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

Grade 9 - Basics of Python

Uploaded by

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

LEARNING OUTCOMES:

AT THE END OF THIS MODULE, STUDNETS


WILL BE ABLE TO:

 Install Python
 Write simple commands in Python
 Understand how to use variables in Python
I. INTRODUCTION
 Python is one of the preferred programming
language used for AI based applications.

 Python is a high level Object Oriented Programming


language (OOP).

 Python was created by Guido Van Rossum, and


released in 1991.

 It is also known as General-Purpose Programming


Language.
I. INTRODUCTION

Because It is utilized in practically every


domain:

 Web development
 Software development
 Game development
 Artificial intelligence
 Machine language
 Data analytics
I. INTRODUCTION

TOP COMPANIES:

 YOUTUBE
 GOOGLE
 DROPBOX
 NETFLIX
 INSTAGRAM
 QUORA

Python is the second most used programming


language in the world as per GitHub.
I. INTRODUCTION

FEATURES OF PYTHON:

1. Easy to learn
2. Readable
3. Standard library
4. Interactive mode
5. Portable
6. Database support
I. INTRODUCTION

1. EASY TO LEARN:

Python has few keywords, simple structure, and a clearly


defined syntax. So it is easy to learn quickly.

2. READABLE:

Python code blocks are identified by indentation.


I. INTRODUCTION

Indentation: It refers to the spaces at the beginning of code


line.

Indentation in Python is very important.

 This makes the Python program highly readable and


easy to maintain.

3. STANDARD LIBRARY:

Most of the Python’s library is very portable and cross-platform


compatible with UNIX, WINDOWS and MAC.
I. INTRODUCTION

4. INTERACTIVE MODE:

Python supports interactive mode which allows interactive


testing and debugging of code.

5. PORTABLE:

Python can run a wide variety of hardware platforms and has


the same interface on all platforms.
I. INTRODUCTION

6. DATABASE SUPPORT:

Python provides an interface to all major commercial database.

DATABASE: Used for storing, maintaining and accessing any


sort of data.

EX: ORACLE, MY SQL, SQLITE


II. LET’S DO PROGRAMMING
IN PYTHON
1. Install Python

2. After install, we have an icon on WINDOWS, or MAC OS


desktop labeled IDLE.

3. Double click on the icon.

TO FIND OUT THE VERSION OF PYTHON:

Use the following command,

import sys ALSO WE CAN USE THE COMMAND PROMPT


print(sys.version) python –V (USE UPPER CASE V)
II. LET’S DO PROGRAMMING
IN PYTHON

SAVING PYTHON FILE:

You need to use an editor to save the Python program that


you are writing. In this case we are using IDLE.

1. To save a new program, open IDLE and choose


File New File

2. An empty window will appear, with “Untitled” in the


menu bar.
II. LET’S DO PROGRAMMING
IN PYTHON
3. Enter the following code into the new window

print(“Hello”)

4. Now, choose File SAVE. Now the Save As dialog box


will appear. Enter the filename. And save the file to your
desktop or any location.

(It will save the file with the extension of .py)

5. Then choose Run Run Module to execute the program.


II. LET’S DO PROGRAMMING
IN PYTHON
Input and output Function:

Every program have an input and an output.

Python used input keyword to get the input from the user
and print keyword to display the output on the screen.

You can try the below program:

name=input(“Enter Your Name:”)


print(name)
II. LET’S DO PROGRAMMING
IN PYTHON
KEYWORD:

Keyword is a word that is reserved by a program because the


word has a special meaning. The user have no rights to
change its meaning.

Keywords are the instructions used by the specific


programming language. We cannot use it as variable names.

Example: print, input, and, break, continue, if, else, elif,


pass, for, while etc.
II. LET’S DO PROGRAMMING
IN PYTHON

SPACES AND INDENTATION:

Indentation in Python refers to the whitespaces at the start


of the line to indicate a block of code. We can create an
indentation using space or tabs.
II. LET’S DO PROGRAMMING
IN PYTHON

The program below will generate a syntax error because of the


space character inserted at the start of the second line.

print (“Hi” )
print (“Learn” )
print (“About Python”)

Spacing out a program in this way is known as


indenting(horizontal spaces). Indentation is part of syntax of
the Python language.
II. LET’S DO PROGRAMMING
IN PYTHON

print(“Hi”)
print(“Learn”)
print(“Python”)

The 3 line program above is correct and will run producing the
expected output.
II. LET’S DO PROGRAMMING
IN PYTHON

Hint:

 If you use the Tab key instead of using spaces for the
purpose of indenting, then you must always use Tab key.

 If you use four spaces, always use the four spaces.

 Mixing tabs and spaces is a syntax error. Even if the


program looks correct on the screen! You will get an
“incorrect indent” error.
II. LET’S DO PROGRAMMING
IN PYTHON

VALUES:

 A value is one of the most basic things in programming.

 A value may be characters i.e. python or number like 1, 2.2,


3.5 etc.

 Values belong to different types: 1 is an integer, 2.2 & 3.5 is a


float and ‘python’ is a string, etc.
II. LET’S DO PROGRAMMING
IN PYTHON
VARIABLES:

 A program uses variables to store the values.

 A variable can hold any value.

 It is like a container to store values.


“Hello” 345

string container Integer container


II. LET’S DO PROGRAMMING
RULES: IN PYTHON
 A variable name must start with a letter or the underscore
character.
 A variable name cannot start with a number.
 A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and_).
 Variable names are case-sensitive(name, Name and NAME
are three different variables).
 The reserved words(keywords) cannot be used for naming
the variable.

EXAMPLE:

number = 10
II. LET’S DO PROGRAMMING
IN PYTHON
REDECLARING A VARIABLE:

 We can change the value of a variable at any point inside a


python program.

#Declaring a variable
Number = 100
#Display
print(“initial value of number is: “, Number)
#Re-declare the variable
Number = 200
print(“value of number after redeclaration:”, Number)
II. LET’S DO PROGRAMMING
IN PYTHON
DATA TYPES:

 It is a very important concept that we have to understand


when writing a program.
 Data types are the classification or categorization of data
items. It represents the kind of value that tells what operations
can be performed on a particular data.
 The Python compiler does not understand that it is a number.
 Any input is considered as a string value by default.
 So when you add two strings, the program just combines the
values; it is called as concatenation.
II. LET’S DO PROGRAMMING
IN PYTHON
DATA TYPES:

We don’t have to define the data type. It will be automatically set


based on the first time use of the variable.

1. Mutable: It can be changed after creating. Example: List, Sets,


Array and Dictionary etc.

1. Immutable: It cannot be changed after creating.


Example: int, str, bool, float, tuple etc.
II. LET’S DO PROGRAMMING
IN PYTHON
Five standard data types:

1. Numbers
2. Strings
3. List
4. Tuple
5. Dictionary

1. Numbers:
Numeric values are stored in number data type.
Python includes three numeric types to represent numbers:
integers, float, complex.
II. LET’S DO PROGRAMMING
IN PYTHON
Numbers:
int(signed integers): In Python, integers are
zero, positive or negative whole numbers without a
fractional part. Ex: 0, 100, -10

float(floating point real value): In Python,


floating point numbers are positive and negative real
numbers with fractional part denoted by the decimal
symbol. Ex: 0.23, -1.45, 323.78
II. LET’S DO PROGRAMMING
IN PYTHON

Numbers:
complex(complex numbers): A complex
number is a number with real and imaginary
components.
Ex: 5 + 2j
we must use j or J as imaginary components.
Using other characters it will show syntax error.
II. LET’S DO PROGRAMMING
IN PYTHON
Integer Float Complex
Example:
17 17.21 17.21j
-10 -17.12 21.17j

We can use function to find the datatype of the variable.


Syntax:
type(variable_name)
Example:
a=12.67
print(type(a))
Output:
<class 'float'>
II. LET’S DO PROGRAMMING
IN PYTHON
Integer Float Complex
Example:
17 17.21 17.21j
-10 -17.12 21.17j

We can use function to find the datatype of the variable.


Syntax:
isinstance(variable_name,datatype)
Example:
a=“Python"
isinstance(a,str)
Output:
True
II. LET’S DO PROGRAMMING
IN PYTHON
Example: Program Output
x=15
print(type(x)) <class ‘int’>

x=15.5
print(type(x)) <class ‘float’>

x=“Cybersquare"
print(type(x)) <class ‘str’>

x=True
print(type(x)) <class ‘bool’>
II. LET’S DO PROGRAMMING
IN PYTHON
Typecasting:

As per the above table, the Python compiler


automatically decides the data type by the value
assigned to it.

But in some cases, we have converted into a


specific data type. Any input is considered as a
string value by default.
II. LET’S DO PROGRAMMING
IN PYTHON
Typecasting:

Type Casting is the method to convert the Python


variable datatype into a certain data type in order
to perform the required operation by users.

Type cast to Code

Integer int(variable_name)
Float float(variable_name)
String str(variable_name)
II. LET’S DO PROGRAMMING
IN PYTHON
2. String:

A string is a sequence of characters enclosed with


in either single quotes (‘ ‘) or double quotes (“ “)
ex:
mystr=“Hello Python”
print (mystr)

O/P:
Hello Python
II. LET’S DO PROGRAMMING
IN PYTHON
3. List:

List are used to store multiple items in a single


variables.

syntax:
<listname>=[ ]

ex: Fruits=[‘mango’, ‘apple’, ‘grapes’]


L[0] L[1] L[2]
II. LET’S DO PROGRAMMING
4. Tuple:
IN PYTHON
Tuples are a type of data structure that is very similar to
list.
Difference is
tuples are immutable
lists are mutable
Tuples are used to store multiple items in a single
variables.
Tuple are created by adding comma separated values
inside the parentheses ( ).
Ex:
tup= (‘ Python’ , ‘SQL’ )
t(0) t(1)
II. LET’S DO PROGRAMMING
IN PYTHON
Program:

# initialize tuple
tup1 = (‘Python’ , ‘SQL’ )
# initialize another tuple
tup2 = (‘Learning’)
newtuple = tup1+tup2
print(newtuple)
II. LET’S DO PROGRAMMING
IN PYTHON
Program:
Ex2:
t1= (0,1,2, “Python”)
t2=(“SQL”, 26,25)
new=t1+t2
print(new)
II. LET’S DO PROGRAMMING
IN PYTHON
Dictionary:

Dictionary is a data structure that allows us to


easily write very efficient code.

d={‘a’:10, ‘b’:20, ‘c’:30}


d[‘a’] d[‘b’] d[‘c’]

Unordered collections of unique values stored in


(key values) pairs.
II. LET’S DO PROGRAMMING
IN PYTHON
Comments:

Comment statements are non executable statement.

Comments are human-readable descriptions in the


source code of a program, which is used to explain
codes and make the code more readable and
understandable.

# - is used for single line comments.


Triple Quotes """ or ''' are used multiline comments
II. LET’S DO PROGRAMMING
IN PYTHON
Operators:

An operator is a symbol which represents some


operation that can be performed on data. They
are

1. Arithmetic operators
2. Relational operators
3. Logical operators
II. LET’S DO PROGRAMMING
IN PYTHON
1. Arithmetic Operators: Arithmetic operators
are used to do arithmetic calculations.
Operator Operation Example Result
+ Addition x=5+3 x=8
- Subtraction y=25-10 y=15
* Multiplication z=5*3 z=15
/ Division a=15/3 a=5
// Floor Division (rounds it to the b=10/4 b=2
nearest integer value)
** Exponentiation (calculate the c=2**3 c=8
power)
% Modulus (calculate remainder d=7%3 d=1
after division)
II. LET’S DO PROGRAMMING
IN PYTHON
2. Relational Operators: Relational operators are used
find out the relationship between two operands.

Operator Operation Example Result

< Less than 3<5 True


<= Less than or equal to 25<=10 False
> Greater than 5>3 True
>= Greater than or equal to 5>=5 True
== Equal to 15==20 False
!= Not equal to 10!=10 False
II. LET’S DO PROGRAMMING
IN PYTHON
3. Logical Operators: Logical operators are used find
out the relationship between two or more relational
expression.

Logical operators are performed on Boolean values.

Logical operations are AND, OR and NOT.


II. LET’S DO PROGRAMMING
IN PYTHON
3. Logical Operators:

X Y X and Y X or Y Not X
True True True True False
True False False True False
False True False True True
False False False False True

Ex: 5>10 and 20<50 becomes False and True, and the
final result becomes False
II. LET’S DO PROGRAMMING
IN PYTHON
Assignment Operators: let us assume that the value of
X is 5.
Value of x
Operator Description Code Meaning after the
operation

=
Simple assignment operator.
Assign values from RHS value X=50 50
to LHS variable

+= Add and Assignment operator.


Value of LHS variable will be
added to the RHS value and is X+=10 X=X+10 15
assigned to LHS variable
II. LET’S DO PROGRAMMING
IN PYTHON
4. Assignment Operators: let us assume that the value
of X is 5.
Value of x
Operator Description Code Meaning after the
operation

-= Subtract and Assignment


operator.
RHS value will be subtracted X-=3 X=X-3 2
from LHS variable and is
assigned to LHS variable.
*= Multiply and Assignment
operator.
Value of LHS variable will be X*=10 X=X*3 15
multiplied by RHS value and is
assigned to LHS variable
II. LET’S DO PROGRAMMING
IN PYTHON
4. Assignment Operators: let us assume that the value
of X is 5.
Value of x
Operator Description Code Meaning after the
operation

/= Divide and Assignment operator.


Value of LHS variable will be
divided by the RHS value and is X/=2 X=X/2 2.5
assigned to LHS variable.

//= Integer Division and Assignment


operator.
Value of LHS variable will be X//=2 X=X//2 2
divided by RHS value and is
assigns the integer part to LHS
variable
II. LET’S DO PROGRAMMING
IN PYTHON
4. Assignment Operators: let us assume that the value
of X is 5.
Value of x
Operator Description Code Meaning after the
operation

%= Modulus and Assignment operator.


Value of LHS variable will be
divided by the RHS value and the X%=2 X=X%2 1
remainder will stored in the LHS
variable.
** Exponentiation and Assignment
operator.
It takes the power of the LHS X**=2 X=X**2 25
value to the RHS value and assigns
the result to the LHS variable
II. LET’S DO PROGRAMMING
IN PYTHON
Operator Precedence:

Operator Description
** Exponentiation (raise to the power)

* / % // Multiply, Divide, Modulus, Floor Division


+- Addition and Subtraction
< <= > >= Comparison operator
== != Equality Operator
= %= /= //= -= += *= **= Assignment Operator
not or and Logical Operator
II. LET’S DO PROGRAMMING
IN PYTHON
Statements:
Statements are the instructions written in any
programming language given to the computer for
executing a specific task.

Expression:
An expression is a combination of operator and operands.
In any programming language, an expression is evaluated
as per the precedence of its operator. If there is more than
one operator in an expression, their precedence decides
which operation will be performed first.

You might also like