Getting Started With Python
Getting Started With Python
1
Chapter 5 Getting Started with Python
Interactive Mode
To work in the interactive mode, type a Python statement on the >>> prompt directly. The
interpreter executes the statement and displays the results.
Script Mode
In the script mode, write a Python program in a file, save it, and then use the interpreter to execute
it. Python scripts are saved as files with the .py extension.
5.3 Identifiers
Identifiers are names used to identify a variable, function, or other entities in a program. Rules for
naming an identifier in Python are:
• The name should begin with an uppercase or a lowercase alphabet or an underscore _.
• It can be of any length.
• It should not be a keyword or reserved word.
• Special symbols like !, @, #, $, %, etc., are not allowed.
Example:
# Invalid identifiers
1marks, mark$2, avg#
5.4 Variables
A variable in Python refers to an object that is stored in memory. Variable declaration is implicit,
meaning variables are automatically declared when assigned a value.
Example:
[ ]: # Variable assignment
length = 10
breadth = 20
area = length * breadth
print(area) # Output: 200
5.5 Comments
Comments are used to add a remark or a note in the source code. They are not executed by the
interpreter. In Python, a comment starts with #.
Example:
[ ]: # This is a comment
amount = 3400 # Variable amount is the total spending on grocery
[ ]: num1 = 20
print(id(num1)) # Identity of num1
num2 = 30 - 10
print(id(num2)) # Identity of num2 and num1 are the same as both refer to␣
↪object 20
[ ]: num1 = 10
print(type(num1)) # <class 'int'>
var1 = True
print(type(var1)) # <class 'bool'>
float1 = -1921.9
print(type(float1)) # <class 'float'>
var2 = -3+7.2j
print(type(var2)) # <class 'complex'>
• List: A sequence of items separated by commas and enclosed in square brackets [].
Dictionary
Dictionary in Python holds data items in key-value pairs. Items in a dictionary are enclosed in
curly brackets { }. Dictionaries permit faster access to data. Every key is separated from its value
using a colon (:) sign. The key: value pairs of a dictionary can be accessed using the key. The
keys are usually strings and their values can be any data type. In order to access any value in the
dictionary, we have to specify its key in square brackets [ ].
5.8 Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
Arithmetic Operators
Example:
[4]: x = 15
y = 4
print(x + y) # 19
print(x - y) # 11
print(x * y) # 60
print(x / y) # 3.75
print(x % y) # 3
print(x ** y) # 50625
print(x // y) # 3
19
11
60
3.75
3
50625
3
Comparison Operators
Example:
[3]: x = 10
y = 12
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
False
True
False
True
False
True
Logical Operators
Example:
[ ]: x = True
y = False
Assignment Operators
Example:
[ ]: x = 5
x += 3 # x = x + 3
print(x) # 8
x -= 2 # x = x - 2
print(x) # 6
x *= 4 # x = x * 4
print(x) # 24
x /= 3 # x = x / 3
print(x) # 8.0
\x %= 5 # x = x % 5
print(x) # 3.0
Identity Operators
Example:
[ ]: num1 = 10
print(id(num1))
num2 = num1
print(id(num2))
print(num1 is num2)
print(num1 is not num2)
Membership Operator
Example:
[ ]: lst = [1,2,3,4]
print( 1 in lst)
print(2 not in lst)
5.9 Expressions
Expressions are combinations of values, variables, operators, and function calls that are interpreted
and evaluated by the Python interpreter to produce a result.
Example:
[ ]: x = 5
y = 10
if x < y:
print("x is less than y")
else:
print("x is not less than y")
5.10 Statements
A statement is an instruction that the Python interpreter can execute. We have already seen the
assignment statement. Some other types of statements that we’ll see include if statements, while
statements, and for statements.
Example:
[ ]: x = 5
y = 10
if x < y:
print("x is less than y")
else:
print("x is not less than y")
5.13 Debugging
Debugging is the process of finding and fixing errors in the code.
Example:
[ ]: # Syntax error
10 = x
print(x)
[ ]: # runtime error
x = 10
y = 0
print(x/y)
[ ]: # logical error
x = 10
y = 20
if x>y:
print("x is less than y")
else:
print("y is less than x")
Exercises
1. Write a program to enter two integers and perform all arithmetic operations on them.
[ ]:
[ ]:
[ ]:
[ ]:
6. Write a program that asks the user to enter their name and age. Print a message addressed
to the user that tells the user the year in which they will turn 100 years old.
[ ]:
7. The formula
(𝐸 = 𝑚𝑐2
) states that the equivalent energy (E) can be calculated as the mass (m) multiplied by the
speed of light
(𝑐 = 𝑎𝑏𝑜𝑢𝑡(3𝑡𝑖𝑚𝑒𝑠108 )𝑚/𝑠)
squared. Write a program that accepts the mass of an object and determines its energy.
[ ]:
8. Presume that a ladder is put upright against a wall. Let variables length and angle store the
length of the ladder and the angle that it forms with the ground as it leans against the wall.
Write a Python program to compute the height reached by the ladder on the wall for the
following values of length and angle:
• a) 16 feet and 75 degrees
• b) 20 feet and 0 degrees
• c) 24 feet and 45 degrees
• d) 24 feet and 80 degrees
[ ]: