0% found this document useful (0 votes)
6 views7 pages

Getting Python

Uploaded by

rajkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Getting Python

Uploaded by

rajkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter – 5 (Getting started with python)

INTRODUCTION TO PYTHON : An ordered set of instructions to be executed by a


computer to carry out a specific task is called a program. As we know that computers
understand the language of 0s and 1s which is called machine language or low level
language. it is difficult for humans to write or comprehend instructions using 0s and 1s.
This led to the advent of high-level programming languages like Python, C++, Visual
Basic, PHP, Java that are easier to manage by humans but are not directly understood
by the computer.

A program written in a high-level language is called source code. compilers and


interpreters are needed to translate the source code into machine language. Python uses
an interpreter to convert its instructions into machine language, so that it can be
understood by the computer. An interpreter processes the program statements one by
one, first translating and then executing.

Features of Python :
1. Python is a high level language. It is a free and open source language.
2. Python programs are easy to understand as they have a clearly defined syntax and
relatively simple structure.
3. Python is case-sensitive. For example, GOOD and Good are not same in Python.
4. Python is also helpful in web development.
5. Python uses indentation for blocks and nested blocks.

Execution Modes : There are two ways to use the Python interpreter:
(a) Interactive mode (b) Script mode

(a) Interactive Mode : in the interactive mode is convenient for testing a single line
code for instant execution. But in the interactive mode, we cannot save the statements
for future use.

(b) Script Mode : In the script mode, we can write a Python program in a file, save it
and then use the interpreter to execute it. Python scripts are saved as files where file
name has extension “.py”.

PYTHON KEYWORDS : Keywords are reserved words. Each keyword


has a specific meaning to the Python interpreter, and we can use a
keyword in our program only for the purpose for which it has been
defined.

1
false class finally is return
none continue for try true
def from while and del
global not with as elif
if or break except in
raise

IDENTIFIERS : In python identifiers are names used to identify a variable, function,


or other entities in a program.
1. The name should begin with an uppercase or a lowercase alphabet. Thus, an
identifier cannot start with a digit.

2. It should not be a keyword or reserved word.


3. We cannot use special symbols like !, @, #, $, %,etc., in identifiers.

VARIABLES : A variable in a program is uniquely identified by a name(identifier).


Variable in Python refers to an object — an item or element that is stored in the
memory.
Example :
gender = 'MALE'
message = 'GOOD MORNING'
price = 987.9

Write a Python program to find the area of a rectangle given that its length is 10 units
and breadth is 20 units.

length = 10
breadth = 20
area = length * breadth
print(area)
Output:
200

COMMENTS
Comments are used to add a remark or a note in the source code. Comments are not
executed by interpreter.
Sometimes comments are needed to understand the working of the program.

2
In Python, a comment starts with # (hash sign). Everything following the # till the end
of that line.

#Variable amount is the total spending on grocery


amount = 3400
#totalMarks is sum of marks in all the tests
#of Mathematics
totalMarks = test1 + test2 + finalTest

DATA TYPES : Data type identifies the type of data values a variable can hold and
the operations that can be performed on that data.

(1) Number : Number data type stores numerical values only. It is further classified
into three different types: int, float.
int(for integer value) – 10,20 float(for decimal value) : 20.56

Boolean data type (bool) is a subtype of integer. It is a unique data type, consisting of
two constants, True and False.

Sequence : The three types of sequence data types available in


Python are
Strings, Lists and Tuples.

(1) String : String is a group of characters. These characters


may be alphabets, digits or special characters including spaces.
String values are enclosed either in single quotation. marks (e.g.,
‘Hello’) or in double quotation marks (e.g.,“Hello”).
Example : Myvalue = 'Hello Friends'

(2) List : List is a sequence of items separated by commas and the items are
enclosed in square brackets [ ].
Example : Mylist = [5, 3.4, 45, 90, 45]
print(Mylist1)

(3) Tuple : Tuple is a sequence of items separated by commas and items are enclosed
in parenthesis ( ).

3
Mytuple = (10, 20, "Apple", 3.4, 'a')
Print(Mytuple)

(4) Set : Set is an unordered collection of items separated by commas and the items are
enclosed in curly brackets { }.
In set it doesn’t allowed the duplicate entries.
Example : set1 = {10,20,3.14,"New Delhi"}

(5) Dictionary : Dictionary in Python holds data items in key-value pairs. Items in a
dictionary are enclosed in curly brackets { }. Every key is separated from its value
using a colon (:) sign.
Example : dict1 = {'Fruit':'Apple','Climate':'Cold', 'Price(kg)':120}

Mutable and Immutable Data Types

Mutable data type : Variables whose values can be changed after they are created
and assigned are called mutable.

Immutable data type : Variables whose values cannot be changed after they are
created and assigned are called immutable.

OPERATORS : An operator is used to perform specific mathematical or logical


operation on values.

Arithmetic Operators : It is mathematical operator used for


calculation.

Operat Description
or
+ Addition operator : used to add values
- Subtraction operator : used to minus values
Multiplication operator : used to multiply
*
values
Modulus operator : used to find remainder of
%
numbers
Division operator : used to find quotient in
/
points
Floor division : used to find quotient without
//
points
Exponent : used4 to find the power of the
**
given number
Relational operator : Relational operator is used to find a relation between operator or
operands.

Operat Description
or
Equalto operator : used to compare the
==
values
Greater operator : used to check greater
>
values
< Less operator : used to check less values
>= Greaterthan equal to : Greater than equal to
<= Lessthan equal to : less than equal to
Logical Operator : There are three logical operators in python(and , or , not).

Operator Description
and operator : It works when all the given
and
condition is true
or operator : It works when a single condition
or
is true
Not operator : it works when the condition is
not
false
Membership Operators :
Operator Description
in operator : It return true if value is found
Example : Myvalue = [1,2,3]
in
print(3 in Myvalue)
True
not in operator : It works when a condition is
false
not in Example : Myvalue = [1,2,3]
print(45 in Myvalue)
True
DEBUGGING : The process of identifying and removing such mistakes, also known
as bugs or errors, from a program is called debugging. In this we have to learn three
types of errors :

5
(1) Syntax error : Syntax errors are invalidities in the code's structure. Python syntax
errors happen when the Python interpreter is unable to recognize the structure of
statements in code.
Example : if we want to print some data we use the print command.
a = 10
if(a==90)
print("This will cause a syntax error")

it gives error because we have forgotten to put colon at the end of line. Now the correct
code is
a = 10
if(a==90):
print("This will cause a syntax error")

Runtime Error : Runtime error is when the statement is correct syntactically, but the
interpreter cannot execute it. Runtime errors do not appear until after the program starts
running or executing.

Example :
firstNum = 11
secondNum = 0
print(firstNum/secondNum)

Traceback (most recent call last):


File "C:\Users\Dell\Desktop\Code.py", line 3, in <module>
print(firstNum/secondNum)
ZeroDivisionError: division by zero

Assignments

Q1. Write a Python program to calculate the amount payable if money has been lent on
simple interest.

Simple Interest(Formula) = (P * R* T)/100


P = Principal R = Rate I = interest

P = int(input(“Enter principal”))
R = int(input(“Enter rate”))

6
T = int(input(“Enter Time”))
SI = (P*R*T)/100
Print(“Simple Interest is “, SI)

Q2. Which data type will be used to represent the following data values ?

1. Mobile Number : - In this we use int data type.


2. Date of birth : Date
3. Address of the student : string or varchar(n)
4. weight of gold : float
5. Record date and time: datetime

You might also like