0% found this document useful (0 votes)
35 views5 pages

Introduction To Python Programming

Uploaded by

Rishta Porwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views5 pages

Introduction To Python Programming

Uploaded by

Rishta Porwal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

ARYA VIDYA MANDIR GROUP OF SCHOOLS

STD VIII- COMPUTERS

Introduction to Python programming

Python is a powerful multi-purpose programming language created by Guido van


Rossum.

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.

1. A simple language which is easier to learn


Python has a very simple and elegant syntax. It's much easier to read and
write Python programs compared to other languages like: C++, Java, C#.
2. Free and open-source
You can freely use and distribute Python. Not only can you use and distribute
softwares written in it, you can even make changes to the Python's source
code.
3. Portability
You can move Python programs from one platform to another, and run it
without any changes. It runs seamlessly on almost all platforms
including Windows, Mac OS X and Linux.
4. Object-oriented
Everything in Python is an object. Object oriented programming (OOP) helps
you solve a complex problem intuitively.
With OOP, you are able to divide these complex problems into smaller sets by
creating objects.

Example 1:

print (“Hello world”)

Example 2:
#Add two numbers

a = 2

b = 3

sum = a + b

print(sum)

Any line starting with # in Python programming is a comment.

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:

1. Single line comments: These starts with a hash symbol(#)

Eg: # This is a program to accept marks of a child

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.

Eg: “”” This is a program to calculate the total of marks


and percentage of a child “””

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

Similarly, 3 is stored in b variable.

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.

Rules for writing identifiers

1. Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to


Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_1 and
print_this_to_screen, all are valid examples.
2. An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly
fine.
3. Keywords cannot be used as identifiers.
4. We cannot use special symbols like !, @, #, $, % etc. in our identifier.
5. It is case sensitive.

Identify the following valid/invalid variable names. If not explain why.

a) r2d2 b) H2O c) 2ndbirthday

d) x(3) e) cost_in_$ f) Roll_No

Need to have variable names :

We need to name the variables for the following reasons :

● To identify / distinguish one variable from another.


● To assign(store) a value to a variable and use the value stored in the variable .
Variables and their data type :

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).

Assigning values to variables :

Values can be assigned to the variables .The syntax for assigning values to variable is :

Variable =value/variable2/arithmetic expression;

Arithmetic Operators and their hierarchy :

An arithmetic operator is a symbol that operates on one or more expressions


producing a result . The six simple arithmetic operators are summarized in the table
below:

Operator Description Example

+ 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 :

a) To print your name, Std-div and school name on different lines.

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.

******

You might also like