Python Fundamentals
Python Fundamentals
def sum_two_numbers(a,b):
c=a+b
return c
d = sum_two_numbers(5,6)
print(“sum of given two number is ”,d)
PYTHON BASICS : Character set, Tokens, Expressions, Statements, Simple input and output etc.
PYTHON CHARACTER SET : It is a set of valid characters that a language can recognize. Python
supports Unicode encoding standard.
Letters : A-Z, a-z
Digits : 0-9
Special Symbols : space, +, -, *, /, **, \, (), [], {}, = , !=, ==, <, > , ‘ ,“, “, ‘, ; , : , %
Whitespaces : Blank space, Tabs, Carriage return, Newline.
Other Characters : Python can process all ASCII and Unicode characters as part of data.
TOKENS / LEXICAL UNITS / LEXICAL ELEMENTS : The smallest individual unit in a program is
known as a token.
Python has following tokens :
a) Keyword : It is a word having special meaning reserved by programming language. They
cannot be used for normal identifier names.
b) Identifiers (Names) : It is defined as names given to different parts of the program (
variables, objects, classes, functions, list, dictionaries etc)
Rules :
First character must be a letter or _ .
Upper case and lower case letters are different.
Digits 0-9 can be a part of the identifier except for the first character.
An identifier must not be a keyword of python.
An identifier cannot contain any special character except __ .
Valid Identifiers : Myfile, Date9_7, _sci.
Invalid Identifiers : Data – Record, 29Science, break, my.file.
c) Literals / Values : They are fixed values or constant values. Python supports several kinds
of literals.
1
String Literal : It is defined as a sequence of characters surrounded by quotes ( single
or double or triple quotes).
Example : ‘Vishu’, “Priyanshu”,’’’Jiya’’’etc.
Python allows certain nongraphic characters in string values.
Nongraphic characters are those characters that cannot be typed directly
from keyboard such as backspace, tabs, carriage return.
Nongraphic characters can be represented by using escape sequences, which
are represented by a backslash ( \ ) followed by one or more characters.
Example : print(‘ \\ ’)
String Types in Python : Python supports two types of strings :
1. Single Line Strings (Basic Strings): It must terminate in one line.
Example : a = ‘ hello
there ‘
Error : By default, python creates single line strings with both single or double quotes. So,
if at the end of a line, there is no closing quotation mark for an opened quotation mark,
python shows an error.
e) Punctuators : Punctuators are special symbols that are used in programming languages to
organize programming – sentence structure, and indicate the rhythm and emphasis of
expressions, statements and program structure.
Example : ‘ , “ , # , ( ), = etc.
EXPRESSIONS : Any legal combination of symbols that represent a value is called an expression.
Example : 15, 56, a+5, (3+5)/ 4
3
STATEMENT : It is a programming instruction that does something i.e some action takes place.
Example : print(5+3) , where ‘print(5+3)’ is a statement and ‘(5+3)’ is an expression
b = a - 5 , where ‘b = a - 5’ is a statement and ‘a-5’ is an expression.
COMMENT : Comments are the additional readable information to clarify the source code. It
begins with symbol #.
Types of comment are as follows :
Full line comment :
# Main program code follows now
Inline comment :
if b < 5 : # checking if b is less than 5
Multi-line comment / Block comment : We can enter multi – line comment in python code
in two ways :
Add a # symbol in the beginning of every physical line.
Example :
#Multiline comments are useful for detailed additional information.
#Related to the program in question.
Type a comment as a triple – quoted multi-line string
Example:
‘’’ Multiline comments are useful for detailed additional information.Related to the
program in question. ‘’’
Docstring : Comments enclosed in triple quotes (“””) or triple apostrophe (‘’’) are
called docstrings. They are very useful for documentation.
FUNCTIONS : It is a block of code that has a name and it can be reused by specifying its name in
the program, where needed.
BLOCK / CODE BLOCK / SUITE AND INDENTATION : A group of statements which are part of a
function are called block.
VARIABLES : It is a named location that refers to a value and whose values can be used and
processed during the program run.
>>> a = 5 >>> student = ‘Jacob’ >>> age = 15
LVALUE AND RVALUE
lvalue : Expressions that can come on the LHS of an assignment or lvalues are the objects to
which we can assign a value or expressions
4
rvalue : Expressions that can come on the RHS of an assignment or rvalues are the literals and
expressions that are assigned to lvalues.
Example : a = 10 , where a is lvalue and 10 is rvalue.
MULTIPLE ASSIGNMENTS
Assigning same value to multiple variables >>> a = b = c = 10
Assigning multiple values to multiple variables >>> x, y, z = 10, 20,30
Swapping two variables >>> x, y = y, x
Note : Expressions separated with commas are evaluated from left to right and assigned in same order.
Example 1: Example 2: Example 3:
a , b, c = 5, 10, 7 x = 10 x , x = 20,30
b, c, a = a+1, b+2, c-1 y, y = x+2, x+5 y, y = x+10, x+20
print(a,b,c) 6,6,12 print(y) 15 print(x,y) 30 50
DYNAMIC TYPING : In dynamic typing, a variable pointing to a value of a certain type, can be made to
point to a value/object of different type.
Example :
x=10
print(x) 10
x= “Hello world”
print(x) Hello world
CAUTION WITH DYNAMIC TYPING
X=10
Y=0
Y=X/2
X=’day’
Y=X/2 Error!! A string cannot be divided
TYPE (variable_name)
>>> a =10 >>>a =10.5 >>>a =”hello”
>>> type(a) >>> type(a) >>> type(a)
<class ‘int’> <class ‘float’> <class ‘str’>
SIMPLE INPUT : Built-in function input( ) is used to take input from the user.
Example :
name = input (‘What is your name ?’)
5
Note : input () always returns a value of string type.
Age = input(“Enter your age : ”)
Age = Age + 1 Error : string cannot be added with an integer
Solution:
Age =(int(input(“Enter your age : ”)))
Age = Age + 1