Introduction To Python Programming
Introduction To Python Programming
It has simple easy-to-use syntax, making it the perfect language for someone trying
to learn computer programming for the first time.
Python is a case-sensitive language. This means, Variable and variable are not the
same. Always name identifiers that make sense.
Example 1:
Example 2:
#Add two numbers
a = 2
b = 3
sum = a + b
print(sum)
Comments are used in programming to describe the purpose of the code. This helps
you as well as other programmers to understand the intent of the code. Comments
are completely ignored by compilers and interpreters. There are two types of
comments:
2. Multi line comments: When we want to mark several lines as comment, then
instead of writing # in the beginning of every line , We put “””(triple double
quotes ) in the beginning and ending of the block.
Line 2: a = 2
Here, a is a variable. You can store a value in a variable. Here, 2 is stored in this
variable.
Line 3: b = 3
Line 4: sum = a+ b
The variables a and b are added using + operator. The result of addition is then
stored in another variable sum.
Line 5: print(sum)
The print() function prints the output to the screen. In our case, it prints 5 on the
screen.
Python Identifiers
Identifier is the name given to entities like class, functions, variables etc. in Python. It
helps differentiating one entity from another.
Variable may contain integer values (i.e. numeric values without decimal position), float
(i.e. numeric values with decimal position ) or char values (i.e. single alphabetic
character or digit any other single character).
Values can be assigned to the variables .The syntax for assigning values to variable is :
+ Add a+b
- Subtract m-n
- Negate -n
* Multiply m*n
/ Divide a/b
% Remainder x%y
Exercise : Write programs in Python for the following :
b) Anita purchases textbooks for Rs 300/- and notebooks for Rs 175/- . calculate and
print the total amount spent.
c) A worker at a construction site earns Rs 50/- per day. if he works on all days in the
month of March , calculate and print his total earnings for the month.
d) If Karan spent Rs215/- on snacks and his mother gave him Rs275/- , calculate and
print how much money Karan has to return to his mother.
e) If 12 pens are purchased at the rate of Rs14 /- calculate and print the total amount
payable to the shopkeeper.
f) Rajan’s mother is thrice his age ; if his mother is 57 years old calculate and print
Rajan’s age .
g) 2 kgs of Cauliflower are purchased at Rs 40/- per kg and 3 kgs beans are purchased
at Rs 30/- per kg, calculate and print the total amount payable to the vegetable vendor.
h) The monthly bill of milk of Rs 600 for a quantity of 30 litres. Print the cost per litre.
i) If the total score of 3 students in G.K. quiz is 228 print the average score.
j) 4 kgs of tomatoes are purchased at Rs 30/- per kg and 2 kgs potatoes are purchased
at Rs 20/- per kg, calculate and print the total amount payable.
******