Class 12 – Computer Science : Python Revision Tour – I
[A] – Basics of Python
Topics are :
- A : Basics of Pythons
- B : Decision Making Statements
- C : Loop and Jump Statements
INTRODUCTION
A python is an object-oriented, interpreted, high-level language and a very powerful programming language.
Developed by Guido Van Rossum in 1991.
INSTALLATION
Download it from www.python.org and install your computer by clicking on the installation file.
WORKING IN PYTHON
In two modes – (i) Interactive Mode and (ii) Script Mode.
TOKENS IN PYTHON
The smallest individual unit in a program is called a token.
TYPES OF TOKEN
(i) Keyword (ii) Identifiers (iii) Literals (iv) Operators & (v) Punctuators
Keyword
The word has a special meaning in a programming language. If, else, while, for, or etc
How to find the list of keywords in Python?
Identifiers
Names that are given to different parts of programs. Variable, Function, Class, etc.
Literals
Data items that have a fixed value. Several types of literal in Python.
(a) String (b) Numeric (c) Boolean (d) None
Operators
Triggers some computation or action when applied to the variables. +, -, / .
Punctuators
Symbols that are used to organize sentence, structures and expressions. ‘ “ # \ ( ) { } @ , : ; ` =, etc.
STRUCTURE OF PYTHON PROGRAM
# This is a single line comment
# Function
def thanks( name ):
print(“Thanks Mr/Ms. “, name , “for watching Anjeev Singh academy”)
#Main Program
a = 20 # variable declaration and intialization
b = 30
c = a + b # expression
print (“Sum of “, a”, “and”, b, “is”, c )
if c > 40 : # start of if block , showing by :
print(“You are eligible for lucky draw”)
else:
print(“Sorry, Better Luck next time….”)
VARIABLES AND ASSIGNMENTS
A Variable is a named memory location, used for storing and manipulating values.
Example : Name = “Rashmi” , Age = 18
DYNAMIC TYPING
A variable pointing to a value of a certain type can be made to point to a value/object of a different type. This is called Dynamic Typing. For example:
N = “Rashmi”
N = 18
MULTIPLE ASSIGNMENTS
Assigning same value to multiple variables. E.g. a = b = c = 10
Assigning multiple values to multiple variables. E.g. a , b , c = 10, 20, 30
INPUT
In python 3.x , input( ) is a built-in function, which allows user to input a value.
in_varibale = input( <prompt message> )
Note : input( ) function always return a value of String type.
int ( ) :- To convert a value in integer. => x = int (input(“A number plz”)).
float() :- To convert a value in float. => f = float (input(“a number plz:”)).
bool( ) :- To convert a value in Boolean. => b = bool (input(“Boolean value plz:”)) .
PRINT / OUTPUT
In python 3.x, print( ) is a built-in function, which allows the user to send output to a standard output device, i.e. monitor.
print( object , [ sep = ‘ ‘ or <separator string>, end = ‘\n’ or <end-string>] )
print (“Hello”)
print (“Your marks is”, 20)
print (“Welcome”, “You”, “All”, sep = ‘===‘)
print( “Mobile”, end=‘ ‘)
print (“9898989898”)
DATA TYPES
Data types are means to identify the type of data and the set of valid operations for it.
There are the following built-in core data types:-
Numbers Data Types
Integers
Integers – Two types of integers in Python
- a) Integer (signed) – number having only integer, either positive or negative
- b) Booleans – represent truth values False and True. Boolean values False and True behave like values 0 and 1, respectively.
Floating Point Numbers
Is used to store numbers with a fraction part. They can be represented in scientific notation. Also called real numbers.
Example : x = 25.036
y = 4.95e3 => 495.00 => 4.95 x 10^3
Complex Numbers
Python allows to store the complex number. Complex numbers are pairs of real and imaginary numbers. They can store in the form of ‘a+bj . Both real and imag are internally represented as float value.
Example : t = 2 + 3j
t.real => gives the real part
t.imag => gives the imaginary part as a float.
Sequence Data Types
Sequence Data Types is an ordered collection of items, indexed by integers (both +ve or –ve). Three types of sequence are –
Strings
The string is a combination of letters, numbers, and special symbols. In Python 3.x string is a sequence of Pure Unicode characters. Unicode is a system, designed to represent every character from every language.
Example : – “abc”, “12536”, “&*)()^%$”, “abcd12365&^%#’
A string is a sequence of characters and each character can be individually accessed using its index position i.e. forward (0,1, 2, …) and backward (-1, -2, -3, .. …)
Lists
The list is a set of comma-separated values of any datatype inside the square brackets. [ ]
Example => [1, 2 , 3 , 4, 5]
[1.5, 2.5, 4.5, 9.8]
[‘x’, ‘y’, ‘z’]
[‘anjeev’, 25, 4589.36, True]
The list can also be accessed through the index position i.e. forward and backward.