8th&9th Python Note
8th&9th Python Note
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 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.
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.
A, B, C = 10.....here the variables A, B, C have the value of 10 which is referred to as Multiple assignment.
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.
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
A. Int
B. float
C. complex (complex numbers)
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-
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:
User 3