You are on page 1of 3

1.

Introduction to Programming using Python


Python is a high-level, interpreted, interactive and object-oriented scripting language.

Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile
your program before executing it. This is similar to PERL and PHP.

Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that


encapsulates code within objects.

Python is a Beginner's Language: Python is a great language for the beginnerlevel programmers and
supports the development of a wide range of applications from simple text processing to WWW
browsers to games.

Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.

1.1. Python Identifiers


A Python identifier is a name used to identify a variable, function, class, module or other object. An
identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and %
within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower
are two different identifiers in Python.

Here are naming conventions for Python identifiers-

A. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
B. Starting an identifier with a single leading underscore indicates that the identifier is private.

2. Python Variable Types


Variables are reserved memory locations to store values. It means that when you create a variable, you
reserve some space in the memory. Based on the data type of a variable, the interpreter allocates
memory and decides what can be stored in the reserved memory. Therefore, by assigning different data
types to the variables, you can store integers, decimals or characters in these variables.

2.1. Assigning Values to Variables


Python variables do not need explicit declaration to reserve memory space. The declaration happens
automatically when you assign a value to a variable. The equal sign (=) is used to assign values to
variables. The operand to the left of the = operator is the name of the variable and the operand to the
right of the = operator is the value stored in the variable. For example

Age = 30………..here ‘30’ is the value assigned to the variable ‘Age’.

A, B, C = 10.....here the variables A, B, C have the value of 10 which is referred to as Multiple assignment.

SMTA THE CHOICE OF RESPONSIBLE PARENTS!

User 1
A, B, C = 1, 2, “John”……here two integer objects with values 1 and 2 are assigned to the variables a and
b respectively, and one string object with the value "john" is assigned to the variable c.

2.2. Standard Data Types


Python has five standard data types-

A. Numbers
B. String
C. List
D. Tuple
E. Dictionary

Python Numbers
Number data types store numeric values. Number objects are created when you assign a value to them.
For example:

X = 1, Y = 10

Python supports three different numerical types –

A. Int
B. float
C. complex (complex numbers)

Here are some examples of numbers

Age = 10, diameter = 15.20, Num = 1 + 4j

NB: A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj,
where x and y are real numbers and j is the imaginary unit.

To verify the type of any object in python use the type () function. For example

Output:

Python Strings
Strings in Python are identified as a set of characters represented in the quotation marks. Python allows
either pair of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:]
) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end. The
plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For
example-

SMTA THE CHOICE OF RESPONSIBLE PARENTS!

User 2
str = 'Hello grade nine Students!'

A. print (str) ……………Prints complete string …………output - Hello grade nine students!
B. print (str[0]) ……….Prints first character of the string …………output - H
C. print (str[2:5]) …….Prints characters starting from 3rd to 5th …………output - llo
D. print (str[2:]) ………Prints string starting from 3rd character …………output - llo grade nine
students!
E. print (str * 2) ……..Prints string two times …………output - Hello grade nine students!Hello grade
nine students!
F. print (str + ". How are you.") # Prints concatenated string…………output - Hello grade nine
students!. How are you.

Python Lists
There are 4 collection data types in python programming, named List, Tuple, Set, and Dictionary. List is
a collection which is ordered and changeable, which are written in square brackets, and allows duplicate
numbers. Lists are the most versatile of Python's compound data types. A List contains items separated
by commas and enclosed within square brackets ([]). The values stored in a list can be accessed using
the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way
to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition
operator. For example to create a list

Output:
We can access list items by referring to the index number. Notice that index value start from 0, means
the first element of a list has an index of 0. So, to print the second item of a list

Output:

SMTA THE CHOICE OF RESPONSIBLE PARENTS!

User 3

You might also like