Python Fundamentals
Python Fundamentals
Python Fundamentals
For Computer Science and Informatics Practices students’
Hopefully, all of you go through the previous uploaded materials. In this material we shall be talking about all basic
elements that a Python program can contain.
Letters :- A – Z, a – z
Digits :- 0–9
Special symbols:- All special symbols available over keyboard
Whitespaces :- Blank space, tabs, carriage return, new line, form feed
Other Characters:- All ASCII and Unicode characters
TOKENS
The smallest individual unit in a program is known as Token or a lexical unit.
Python has following tokens:
(i) Keywords (ii) Identifiers (iii) Literals (iv) Operators (v) Punctuators/Delimiters
Keywords:-
A keyword is a word having special meaning reserved by programming language.
These words are reserved for special purposes and must not be used as normal identifier names.
Python programming language contains the following keywords:
Identifiers:-
Identifiers are the names given to different parts of the program like variables, objects, classes, functions, lists,
dictionaries etc.
Identifier forming rules of Python are specified below:
An identifier is an arbitrarily long sequence of letters and digits.
The first character must be a letter or underscore ( _ ).
Upper and lower case letters are different as Python is a case sensitive language.
An identifier must not be a keyword
An identifier cannot contain any special character except underscore ( _ )
Examples of some valid identifiers:
Literals:-
Literals (often referred to as constant values) are data items that have a fixed value.
Python allows different kind of literals:
(i) String literals (ii) Numeric literals (iii) Boolean literals (iv) Special literal- None
(v) Literal collections
String Literals:-
The text enclosed in single or double quotes (sometimes triple quotes also) forms a string literal in Python.
Example:
‘Ram’ “Rahim” “12345” ‘123-456’ ‘11FB22D’
Python allows:
(i) Single line strings – created by enclosing text in single quotes or double quotes and must terminate in one line.
Example:- ‘ram’ “ram”
(ii) Multiline strings – text spread across multiple lines.
Multiline strings can be created in two ways:
(a) by adding a backslash ( \ ) at the end of the first line:-
Example:- ‘ram\
kumar’
OR
“ram\
kumar”
(Note:- backslash ( \ ) is must at the end of first line. it considered the text as continuous.)
(b) by typing a text in triple quotation marks:-
Example: ‘‘‘ how
are
you’’’
OR
“ “ “ how
are
you” ” ”
Python allows you to have certain nongraphic characters in string values. Nongraphic characters are those characters
that cannot be typed directly from keyboard like backspace, tabs, carriage return etc. (No characters is typed when
these keys are pressed, only some action takes place). These nongraphic characters can be represented by using
escape sequences.
Escape sequences represents a single character with a special meaning and have special purpose. An escape
sequence is represented by a backslash ( \ ) followed by one or more characters. It consumes one byte in
ASCII representation.
Escape sequences in Python:-
Example:-
(Explanation: on the above statement we used ‘\n’, ‘\n’ is an escape sequence used for creating a new line)
(Explanation: on the above statement we used ‘\n’ for creating a line and ‘\t’ for tab; Both are escape
sequences in Python)
Numeric Literals:-
Examples of different numerical types are:-
Examples of integer type literals- (whole numbers without any fractional part):-
25 1234 -31 +97 etc.
Examples of floating point literals –
(i) Fractional form:-
Examples:- 17.5 -23.25 .25 23.0
(ii) Exponent form:-
Examples:- 152E+8 -0.172E-3
(Note :- ‘E’ indicate the exponent part and it cannot be fractional form)
Examples of complex number literals:- (consists of real part and imaginary part(j or J))
Examples:- 3+4j 3+6J
Boolean Literals:-
A Boolean literal can have any of the two values: True or False.
None is used to specify to that field that is not contains any value.
Eg:
Literal collections:-
Collections such as Tuples, Lists and Dictionary are used in Python. These are used to store collection of values in
Python. (There are separate chapters on Lists, Tuples and Dictionaries in your syllabus….so those collections will
discuss later)
Operators:-
Operators can be defined as symbols that are used to perform operations on operands.
Example:
Types of Operators in Python:-
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic operators:-
We discuss the following operators based on two variables; variable a = 10 and variable b = 20
Assignment operators:-
Bitwise operators:-
Identity operators:-
is - returns True if two variables points the same object, otherwise False
is not - returns True if two variables points different objects, otherwise False
(The detail discussions on different operators with examples is in ‘Data Handling’ topic as per your
syllabus……so, I will discuss these details with examples in the material of ‘Data Handling’ in Python(next
material).)
Punctuators / Delimiters:-
Punctuators are symbols that are used in programming languages to organize programming-sentence structures, and
indicate the rhythm and emphasis of expressions, statements and program structure.
‘ “ # \ ( ) [ ] { } @ , . : ` =
BAREBONES OF A PYTHON PROGRAM:-
The following program code is used to illustrate the basic structure of a Python program.
(Don’t worry if the following things are not clear to you at that moment………….They’ll become clear when
we discuss further about these elements later on…..)
As you can see that the above sample program contains various components like:
expressions, statements, comments, function blocks and indentation
(i) Expressions:-
An expression is any legal combination of symbols that represents a value.
Examples:
15
2.8
a+5
3+4/2
a>5 etc.
(ii) Statements:-
Instruction that does something.
Examples:
a = 20
print(“hello”) etc.
(iii) Comments:-
which is readable for programmer but ignored by python interpreter means that statement will not execute.
i. Single line comment: Which begins with # sign.
ii. Multi line comment : either write multiple line beginning with # sign or use triple quoted multiple line.
E.g.
# this is single line comment
‘‘‘ this
is
multiline comment’’’
(iv) Functions:-
a set of instructions that has some name and it can be reused.e.g. mymessage( ) in above program.
(The details of Functions will be discuss later on……)
Creating a variable:-
x = 50
Example:-
a = 50
b = 10
Multiple assignments:-
a = b = c = 10
It will assign value 10 to all three variables a, b, c
x, y, z = 20, 50, 5
It will assign the values order wise i.e. first variable is given the first value, second variable is given second values
and so on……….So, x becomes 10, y becomes 50 and z becomes 5
Try to solve:-
“it’s” - size is 4
“““Ram
Roy””” - size is 7. it is triple quoted multi-line string. So End Of Line(EOL) character ( ) is present
just after the first line.( as you pressed Enter key after 1st line)
“Ram\
Roy” - size is 6. It is a multi-line string created with \ symbol.
Program Tasks:-
1. Write a program to calculate the addition, subtraction, multiplication and division of two user given numbers.
2. Write a program that accepts the marks of 5 different subjects from the user then calculate and display Total marks
and percentage.
3. Write a program to ask for your height in centimeters and then converts your height to feet and inches.
(hints: 1 foot = 12 inches and 1 inch = 2.54 cm)
4. Write a program to ask the price of an item and the discount rate from the user then calculate the net amount.
5. Write a program that accept the Number of sixes, number of Fours and number of Singles of a player then
calculate his total runs.
6. Write a program which accepts two numbers from the user and store it in two variables then interchange (swap)
the values of two variables.
(Hints: let, x = 15 and y = 37 then after interchanging your program display x = 37 and y = 15