Getting Started With Python
Getting Started With Python
1
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.
Chapter 5 Getting Started with Python
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:
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:
Chapter 5 Getting Started with Python
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:
• 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 [ ].
[ ]: dict1 = {'Fruit':'Apple','Climate':'Cold', 'Price(kg)':120}
print(dict1['Price(kg)'])
Chapter 5 Getting Started with Python
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
Operator Description Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Example:
60
3.75
3
5062
5
3
Comparison Operators
Operator Example
== Equal to x == y
!= Not equal to x != y
OperatorDescription Example
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Example:
Chapter 5 Getting Started with Python
Fals
e
True
Fals
e
True
Logical Operators
Operator Description Example
and True if both operands are true x and y
or True if either operand is true x or y
not True if operand is false not x
Example:
Assignment Operators
Example:
Identity Operators
Example:
Chapter 5 Getting Started with Python
Membership Operator
Operator Description Example
in Returns True if the variable/value is found lst = [1,2,3,4], 1 in lst
in the specified sequence and False
otherwise
not in Returns True if the variable/value is not 2 not in lst
found in the specified sequence and False
otherwise
Example:
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:
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:
Chapter 5 Getting Started with Python
5.13 Debugging
Debugging is the process of finding and fixing errors in the code.
Example:
Chapter 5 Getting Started with Python
Exercises
1. Write a program to enter two integers and perform all arithmetic operations on them.
[ ]:
2. Write a program to swap two numbers using a third variable.
[ ]:
3. Write a program to swap two numbers without using a third variable.
[ ]:
4. Write a program to find the average of three numbers. 5. The volume of a sphere with radius r is
( 𝜋𝑟3)
. Write a Python program to find the volume of spheres with radius 7cm, 12cm, 16cm, respectively.
[ ]:
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)𝑚/𝑠)
Chapter 5 Getting Started with Python
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
[ ]: