Unit II
Introduction to python
Python was developed by a Dutch programmer Guido van Rossumat the National research
institute for Mathematics and C. Sc. in Netherlands, early 1990s.
● Rules or syntax of Python:
print() is used to display the data on the output screen
1. Python is a case sensitive language
2. Strings if used in print() function should be enclosed in quotation marks.
● Data types:
- Used to store different types of data.
- Defines the type of operations to be performed on the data.
- There are different data types like numbers, strings.
● Variables in python
A Python variable is a reserved memory location to store values. In other words, a
variable in a python program gives data to the computer for processing.
• Rules for naming a variable
1. A variable name must start with a letter or underscore.
2. A variable name cannot start with a number.
3. Variable names are case-sensitive (age, Age and AGE are three different variables)
4. Variable name can be of any length.
5. It can be the combination of lowercase letter(a-z),upper case letter(A-Z),digits(0-9) and
underscore(_).
6. Keywords cannot be used as variables.
• Assigning values to variable
Storing a value in memory and the operator is (=).
For example:
a=5 assigning an integer value.
b=12.5 assigning a floating point value.
c=”Ram” assigning a string value.
• Printing a value of a variable:
print() is Used to display the value stored in a variable.
Eg.
a=25
print(a)
will print output as 25.
Example Programs
1) Write a program to add two numbers using variables
2) Write a program to print magic words using print()
3) Write a program to find the square of a number using the variables
4) Write a program to find the product of 3 numbers using variables
5) Write a program to print any two favourite subject using variables
6) Write a program to find the remainder of 2 numbers 12,5 using variables
● Python Operators
Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
1. Assignment operators
2. Arithmetic operators
3. Comparison operators
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example
= x=5
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
Python Comparison Operators
Comparison operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Exercise
Fill in the blanks.
1. _________________ is one of the basic function ,which is used to display
information on the monitor.
2.Operator used for assigning value to a variable is _________________.
3. print (“hello”), here _____________ is the argument of the function.
4. _______ and ________ are the two types of built- in data types in python.
5. _______________ is the extension of python file.
6. The % operator is called the _____________ operator.
7. The < operator belongs to ____________ operator type.
State True or False.
1. The extension Python file is .py
2. Phone# is a valid variable name.
3. = = is an arithmetic operator.
Answer the following.
1 ) List down the rules for identifying valid variable name
2) Explain different types of operators in python.
3) How can you assign a floating point value to a variable. Give one example.
Lab activity.
1) Write a program to find the area of rectangle (Area = L x B)
2) Write a program a program to find area of a triangle with base=20 and height=10
3) Write a program to print any two subjects and its marks and find total using variables
4) Write a program for the following output and find the total
Item Price
Book 100
Pen 5
Total
Application Based Questions.
1. Mariam wants to convert rupees to dirhams Help her to do that with a Python progam
[ Hint: 1 dirham=20 rupees].
2. Naman want to solve a mathematical problem to calculate simple interest for
Principal(P)=15000,Rate(R)=5 % and Number of years(N)=4.
[ Hint: Simple Interest SI= P x N x R]