BASICS OF PYTHON
PROGRAMMING
INFORMATICS PRACTICES- UNIT I CHAPTER 1
Python Character Set
A set of valid characters recognized by python.
Python uses the traditional ASCII character set.
The latest version recognizes the Unicode character set. The ASCII
character set is a subset of the Unicode character set.
Letters :– A- Z, a- z
Digits :– 0-9
Special symbols :– Special symbol available over keyboard
White spaces:– blank space,tab,carriage return,new line, form feed
Other characters:- Unicode
INPUT AND OUTPUT
OUTPUT:
Final outcome of a program that is usually displayed on the screen.
To get output displayed, python uses print() method.
Eg:
>>> a=10
>>> b=20
>>>print(a+b)
30
>>> print(‘hello’)
hello
INPUT AND OUTPUT
INPUT:
python allows input() and raw_input() to feed user
information or data to a program.
>>> a= input(“enter a value”)
Hi
>>> b= raw_input(“enter your name”)
Hello
>>>print(a, ‘+’, b)
Hi + Hello
Token
Smallest individual unit in a program is known as token.
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
Keywords
Keywords are words that have a special meaning.
Reserved words of the compiler/interpreter which cannot be used
as identifier.
and or if input while
break continue global lamda not
exec finally import is in
print try class else def
Identifiers
A Python identifier is a name used to identify a variable, function,
class, module or other object.
Also called as variables
Nomenclature/ rules for naming an identifier:
An identifier name can contain characters (A-Z, a-z) and 0-9.
An identifier should not begin with a number.
It cannot contain any special symbol except underscore.
Identifier must not be a keyword of Python.
Python is a case sensitive programming language.
To do
List the valid and invalid identifier names
1. IP
2. Add12
3. name*
4. easy_name
5. For
6. 78abc
7. whilei
Literals
Literals in Python can be defined as number, text, or other data that
represent values to be stored in variables.
Types:
1. Numeric literal- numbers
2. String literal-characters
3. Boolean literal
Numeric literal
Numeric literal
integer literal- whole number
Eg: 10,457, 89, 0
>>> a= 5
floating point literal- decimal number
Eg: 10.2, 548.5
complex numbers
Numbers that have a real and imaginary part often in the form “a+bj”
where a is the real and b is the imaginary part
String literal
1. Character:
Eg: ‘a’, ‘b’, ‘x’, ‘5’, ‘@’
2. string:
Eg: ‘abcd’, “bing25#”, “G00gle”
Boolean literal
1. Boolean literal: True/ False
2. special literal: None
Empty value
Literals- escape sequence
Escape sequence is combinations of characters that has a meaning
other than the literal characters contained therein
operators
Operators can be defined as symbols that are used to perform
operations on operands.
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Operators- Arithmetic Operators.
Arithmetic Operators are used to perform arithmetic operations like
addition, multiplication, division etc.
Program- to find the sum of two
numbers
>>> a= 10
>>> b=20
>>> c=a+b
>>> print(c)
Operators- Relational Operators
Relational Operators are used to compare the values
Operators- assignment operators
Used to assign values to the variables.
a=b
a=a+b
a=a/b
a=a*b
a=a-b
a=a%b
a=a//b
a=a**b
Operators- logical operators
Logical Operators are used to
perform logical operations on the
given two variables or values.
>>> a=30
>>> b=20
>>> if(a==30 and b==20):
…. print('hello')
Output :-
hello
OPERATORS- Membership
Operators
Membership operators in python are used to check if a value is
present in a sequence like list, tuple, dictionary.
eg:
>>> lis=[1,2,3,4,5]
>>> a=4
>>> a in lis
True
Operators- Identity Operators
Identity operators are used to compare the memory location of two
objects.
Operators Precedence
The order in which the operators are executed is called operator
precedence.
Punctuators
Punctuators are symbols used to implement the structure of a
syntax.
* = + @ ^
() [] {} # :
“ , . / -
% ~ \ | &
Barebones of a python program
A python program contain the
following components #sum of 2 no
a. Expression
def sum(a,b)
c=a+b
b. Statement
print(c)
c. Comments
X=10
d. Function
Y=20
e. Blockindentation If(True):
sum(x,y)
Barebones of a python program
Expression : which is evaluated and produce result.
E.g. (20 + 4) / 4
Statement :an executable line in a program.
e.g a = 20 print("Calling in proper sequence")
Comments : which is readable for programmer but ignored by python interpreter.
i. Single line comment: Which begins with # sign.
ii. Multi line comment (docstring): either write multiple line beginning with # sign or
use triple quoted multiple line.
E.g. ‘’’this is my first python multiline comment ‘’’
Function: a code that has some name and it can be reused.
e.g. sum() in above program
Block & indentation : group of statements is called a block. Indentation at same level
create a block.
e.g. all 3 statement of sum() function
Variables
Variable is a name given to a memory location. A variable can be considered as a
container which holds any value.
Python is a type- infer language i.e., you don't need to specify the datatype of
variable. Python automatically gets variable datatype depending upon the value
assigned to the variable.
E.g
name = ‘python' # String Data Type
sum = None # a variable without value
a = 23 # Integer
b = 6.2 # Float
Multiple Assignment
Multiple Assignment: assign a single value to many variables
single value to multiple variable:
a=b=c=1
multiple value to multiple variable:
a,b = 1,2
Swapping:
a,b = b,a #