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

5.Getting Started With Python

Chapter 5 provides an introduction to Python, highlighting its features such as being high-level, interpreted, and case-sensitive. It covers essential concepts including keywords, identifiers, variables, data types, operators, expressions, input/output functions, and debugging techniques. The chapter emphasizes Python's simplicity and readability, making it a suitable choice for beginners in programming.

Uploaded by

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

5.Getting Started With Python

Chapter 5 provides an introduction to Python, highlighting its features such as being high-level, interpreted, and case-sensitive. It covers essential concepts including keywords, identifiers, variables, data types, operators, expressions, input/output functions, and debugging techniques. The chapter emphasizes Python's simplicity and readability, making it a suitable choice for beginners in programming.

Uploaded by

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

CHAPTER 5: GETTING STARTED WITH PYTHON

Python is high-level, object-oriented programming language which is interpreted.

Note: C++ and Java are other object-oriented programming languages.

Features of Python
1. Python is a high-level language.
2. It is an interpreted language.
3. Python programs are easy to understand as they have well defined syntax and relatively
simple structure.
4. Python is case-sensitive.
5. Python has a rich library of predefined functions.
6. Python is portable and platform independent.

PYTHON KEYWORDS
Keywords are reserved words which have a specific meaning to the Python interpreter.
Eg. True, False, break

IDENTIFIERS
They are the names used to identify a variable, function, or other elements in a program.
Rules for naming an identifier
• The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_).
This
may be followed by any combination of charactersa–z, A–Z, 0–9 or underscore (_).
• It can be of any length.
• It should not be a keyword or reserved word
• Special symbols cannot be used.

VARIABLES
Variables are containers for storing data. A variable in a program is uniquely identified by a
name.
The data present in variable can also be called as Object.
Object — An item or element that is stored in the memory of a variable.
Eg. a=10
In the above example a is a variable and 10 is a data/object.

Comment
Comments are used to add a remark or a note in the source code.
The symbol # is used to indicate a comment.
Eg.
#To find the sum of two numbers  this line is a comment and hence will not be executed.
num1 = 10
num2 = 20
result = num1 + num2
Multi-Line Comment:
Use of Hash (#) multiple times in a program to add remarks.
Eg.
#This is a comment
#written in
#more than just one line
print("Hello, World!")

ID of an object
Every value or data item whether numeric, string, or other type is an object.
Every object in Python is assigned a unique identity (ID).
ID is similar to address of a memory location.
Eg. num1 = 20
id(num1)
1433920576 #identity of num1

DATA TYPES

1. Number: Number data type stores numerical values only.


a) int: Represents integer numbers Eg. –12, –3, 0, 125, 2
i) Bool: It is a unique data type, consisting of two constants, True and False.
b) float: Represents real or floating-point numbers Eg. –2.04, 4.0, 14.23
c) complex: Represents complex numbers Eg. 3 + 4j, 2 – 2j
2. Sequence: Ordered collection of items, where each item is indexed by an integer.
a) String: String is a group of characters.
Eg. str1 = 'Hello Friend'
b) List: List is a sequence of items separated by commas and the items are enclosed in square
brackets [ ].
Eg. list1 = [5, 3.4, "New Delhi", "20C", 45]
c) Tuple: Tuple is a sequence of items separated by commas and items are enclosed in
parenthesis (). Once created, we cannot change the tuple.
Eg. tuple1 = (10, 20, "Apple", 3.4, 'a')
3. Set: Set is an unordered collection of items separated by commas and the items are enclosed
in curly brackets { }. It cannot have duplicate entries. Once created, elements of a set cannot
be changed.
Eg. set1 = {10,20,3.14,"New Delhi"}
4. None: None is a special data type with a single value. It is used to signify the absence of
value in a situation.
Eg. myVar = None
5. Mapping: Mapping is an unordered data type in Python.
a) Dictionary: Dictionary in Python holds data items in key-value pairs. Items in a
dictionary are enclosed in curly brackets { }.
Eg. dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}

Mutable and Immutable Data Types


Mutable: Variables whose values can be changed after they are created and assigned are called
mutable.
Immutable: 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.
Operands: The values that the operators work on are called operands.
Eg a+b
In the above example, a and b are operands and + is a operator.

1) Arithmetic Operators:
a) Addition(+): Adds the two numeric values Eg. num1 + num2
b) Subtraction(-): Subtracts the operand on the right from the operand on the left.
Eg. num1 - num2
c) Multiplication (*): Multiplies the two values. Eg. num1 * num2
d) Division(/): Divides the operand on the left by the operand on the right and returns the
quotient.
Eg. num1 = 8 num2 = 4
num2 / num1
0.5
e) Modulus(%) : Divides the operand on the left by the operand on the right and
returns the remainder
Eg. num1 = 13
num2 = 5
num1 % num2
3
f) Floor Division (//) or Integer division: Divides the operand on the left by the operand on
the right and returns the quotient by removing the decimal part.
Eg. num1 = 13
num2 = 4
num1 // num2
3
g) Exponent(**) : Performs exponential (power) calculation on operands.
Eg. num1 = 3
num2 = 4
num1 ** num2
81
2. Relational Operators
Let’s assume num1 = 10, num2= 0, num3 =10 to demonstrate the examples for the following
operators.
a) Equals to(==) : If the values of two operands are equal, then the condition is True, otherwise
it is False
Eg. num1 == num2
False
b) Not equal to(!=) If values of two operands are not equal, then condition is True,otherwise
it is False.
Eg. num1 != num2
True
c) Greater than(>): If the value of the left-side operand is greater than the value of the rightside
operand, then condition is True, otherwise it is False.
Eg. num1 > num2
True
d) Less than (<): If the value of the left-side operand is less than the value of the rightside
operand, then condition is True, otherwise it is False.
Eg. num1 < num3
False
e) Greater thanor equal to(>=) : If the value of the left-side operand is greater than or equal
to the value of the right-side operand, then condition is True, otherwise it is False
Eg. num1 >= num2
True
3. Assignment Operators
a) = Assigns value from right-side operand to left side operand
Eg. num1 = 2
num2 = num1
num2
2
b) += It adds the value of right-side operand to theleft-side operand and assigns the result to
the left-side operand
Note: x += y is same as x = x + y
Eg. num1 = 10
num2 = 2
num1 += num2
num1
12
c) -= It subtracts the value of right-side operand from the left-side operand and assigns the
result to left-side operand.
Eg. num1 = 10
num2 = 2
num1 -= num2
num1
8
d) *= It multiplies the value of right-side operandwith the value of left-side operand and
assigns the result to left-side operand.
Eg. num1 = 2
num2 = 3
num1 *= 3
num1
6
e) %= It performs modulus operation using two operands and assigns the result to left-side
operand.
Eg. num1 = 7
num2 = 3
num1 //= num2
num1
2
4. Logical Operators
a) Logical AND: Only if both the operands are True, then condition becomes True
Eg.
True and True=True
True and False= False
b) Logical OR : If any of the two operands are True, then condition becomes True
Eg.
True or True=True
True or False= True
c) Logical NOT: Used to reverse the logical state of its operand.
Eg. not True= False
not False= True
5. Identity Operators
Identity operators are used to determine whether the value of a variable is of a certain type or
not.
a) is : Evaluates True if the variables on either side of the operator point towards the same
memory location.
Eg. num1 = 5
num2 = num1
id(num1)
1433920576
id(num2)
1433920576
num1 is num2
True
b) is not: Evaluates to False if the variables on either side of the operator point to the same
memory location
Eg. num1 is not num2
False
6. Membership Operators
Membership operators are used to check if a value is a member of the given sequence or not.
a) in : Returns True if the variable/value is found in the specified sequence and False otherwise.
Eg. a = [1,2,3]
2 in a
True
'1' in a
False
b) not in : Returns True if the variable/value is not found in the specified sequence and False
otherwise.
Eg. a = [1,2,3]
10 not in a
True
1 not in a
False

EXPRESSIONS
An expression is defined as a combination of constants, variables, and operators.
The following represents a valid expression
(i) 100
(ii) 3.0 + 3.14
(iii) num
(iv) 23/3 -5 * 7(14 -2)
(v) num – 20.4
(vi) "Global" + "Citizen"
Note:
a) Parenthesis can be used to override the precedence of
operators. The expression within () is evaluated first.
b) For operators with equal precedence, the expression is
evaluated from left to right.
Evaluation of Expressions
1. 20 + 30 * 40
=20 + (30 * 40)
= 20 + 1200
= 1220
2. 20 - 30 + 40
= (20 – 30) + 40
= -10 + 40
= 30
3. 15.0 / 4 + (8 + 3.0)
= 15.0 / 4 + (8.0 + 3.0)
= 15.0 / 4.0 + 11.0
= 3.75 + 11.0
= 14.75

INPUT AND OUTPUT


INPUT : The input() function prompts the user to enter data.
The syntax for input() is:
input ([Prompt])
Prompt is the string we may like to display on the
screen prior to taking the input, and it is optional.
Eg. name = input("Enter your first name: ")
Enter your first name: Arnab
OUTPUT : Python uses the print() function to output data to standard output device — the
screen(Monitor)
Syntax. print(‘string’) or print(value)
Eg.
1. print("Hello")
Output: Hello
2. print(2+3)
Output: 5

DEBUGGING
The process of identifying and removing bugs or errors, from a program is called debugging.
Types of Errors:
i) Syntax errors
ii) Logical errors
iii) Runtime errors
i) Syntax errors: Error is the structure and grammar of the program.
ii) Logical errors: A logical error is a bug in the program that causes it to behave incorrectly.
iii) Runtime errors: Error occurring during the run time or execution time is called as Runtime
errors.

You might also like