Python Programming Fundamentals
Tokens
Token – It is the smallest element/unit of Python program
Identifiers The name of any variable, constant, function, module
Literal A fix numeric or non-numeric value
Token Category
Operators Symbols that perform some kind of operation (example +, - , * , / )
Symbols which can be used as separators of values or to enclose some
Delimiters
values (example {} , [] )
Keywords These are reserved words in Python
Token Category
First_Name variable / identifier
X variable / identifier
print Keyword
= Operator / Delimiter
372 Literal
“Python” Literal
[1,2,3,4,5] [] Delimiter
Variable & Types
A variable is like a container that stores values. This value you can
access and change
Variable
Variable is used to provide a name to an object
(object-every element in Python is termed as an onject)
Example X=150 (X here is a variable holding value 150)
X = 150
Variable assignment operator value
Identity
Variable Components
1. It refers to the memory location address
2. This memory location doen not chane once created
3. id() is used to check the memory location of an object/variable
Data Type - refers to the data type of value it holds
Value - refers to the vaue it holds
In the below given example:
Variable = X
Identity of variable X = 1482682624
Data Type of X = integer (as it holds integer value)
Value of X = 150
It must start either with alphabet (capital or small) or underscore ( _ )
Variable Declaration Rules
It can not start with a number
It can contain only alpha-numeric characters (A-Z, a-z, 0-9)and underscore ( _ )
Python keywords can not be used as a variable name
It can not contain spaces
Variable names are case-sensitive (name,Name and NAME are 3 different variables)
Valid variable name examples Invalid variable name examples
Student1 1student
_student #Rollno
First_Name First Name
awhile while
DOB D.O.B.
Assigning Value to a Variable
➢ Assigning same values to multiple variables
Jan, Mar, May, July, Aug, Oct, Dec = 31
➢ Assigning multiple values to multiple variables
Jan, Feb, Mar, April, May = 31, 28, 31, 30, 31
A variable is not created until some value is assigned to it
Expression
An Expression is a combination of operand and operator.
Example: 10 + 50
operand operator
Operators
Operators are special symbols which represents computation
Operators
Shorthand
Arithmetic Relational Logical
Assignment
Solve arithmetic or These are comparison and , or , not A combination of binary
algebraic expression operators and assignment operator
Arithmetic Operators on Number data
Arithmetic Operators on Number & String data
Relational Operators on Number & String data
Shorthand Assignment Operators on Number data
Operators Symbols
Arithmetic Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Remainder (%)
Exponent (**)
Integer Division (//)
Relational Less than (<)
Greater than (>)
Less than equal to (<=)
Greater than equal to (>=)
Not equal to (<>) , (!=)
Equal to (==)
Logical And
Or
Not
Shorthand Assignment +=
-=
*=
/=
**=
Data Type
Data Types
Number Sequence Mapping Sets Boolean
Integer / Float String / Tuple Dictionary Set Boolean
/ Complex / List
Data Type
Mutable Immutable
List Integer
Dictionary Float
String
Tuple
Boolean
A mutable object can be changed after it is created, and an immutable object can't
Data Type Definition Examples
Number Integer whole number without fraction 67999, -5677, 564
Float Floating numbers are written with 656.89, -676.5, 895.0
decimal point
Complex Complex numbers are made up of pair 5+7j
of real and imaginary number
Sequence String It represents sequence of characters ‘python’
enclosed within quotation marks “python”
(single, double or triple) ‘’’python’’’
Tuple Sequence of objects that cannot be (1,2,3,4,5)
changed (‘a’,’e’,’i’,’o’,’u’)
List Sequence of objects that can be [1,2,3,4,5]
changed [‘a’,’e’,’i’,’o’,’u’]
Mapping Dictionary It is a combination of key and its {1:’Amita’,2:’Sachin’}
value
Boolean Boolean Represents one of two possible values
– True/False
type() can be used to check the data type of a value that a variable hold
Comments
comments:
➢ comments are python statement that are ignored by Python interpreter
➢ comments increase the readability of the code
➢ comments in Python starts with hash symbol (#) and extends till the end of line
Revision Assessment Link:
[Link]
hN9qGjESG4V7KiNJ7n3hPiO6WI7Ky2qNUOUc3WEVONFgwNDlWR080OUk4SEg1MEhB
QS4u
User Input
input ()
➢ input () is used to get the input data from user while working with script mode
➢ input () always returns a string type data
Output of the above program will be:
int ()
➢ int () is used with input () to convert user given value into int type.
Output of the above program will be an integer value (69):
float ()
➢ float () is used with input () to convert user given value into float type.
Output of the above program will be a float value (69.0):
eval ()
➢ eval () is used with input () to convert user given value into a number to
evaluate the string data entered.
Output of the above program will be number data (69):
User Defined Functions
Function (user defined)
➢ Function is a group of statements defined by the user to perform a specific task.
➢ A user defined function can be created by def statement followed by a
parentheses () along with parameters.
➢ def is a keyword in Python
➢ Parameter is a variable that holds the data to be processed
➢ Function statement line ends with colon :
➢ A user-defined function is written in script mode and can be called either in
interactive or script mode itself.
Calling Rect_Area() in Interactive Mode:
Output of the above program will be :
As Python is case-sensitive, if you write rect_area instead of Rect_Area then Name Error
will be generated as shown above (in red colour)
Calling Rect_Area() in Script Mode:
Output of the above program will be:
Indentation
Indentation:
➢ Indentation is shifting of statement to the right of another statement
➢ In Python indentation is used to define a block of statements
➢ To define beginning and end of a function, Python rely on indentation
➢ If you don’t give indentation to define a block of statement/statements, it give
IndentationError
To remove this kind of error, we give indentation.
Revision Assessment Link:
[Link]
hN9qGjESG4V7KiNJ7n3hPiO6WI7Ky2qNUOUtOQTVGQjEwTERRWjBQN0VQUzRPOE5RR
y4u
Solved Questions
1. What is Python Syntax?
Python syntax refers to a set of rules that defines how users and the system should
write and interpret a Python program.
2. What is dynamic typing?
Python supports dynamic typing means whatever value we assign to the variable; the
data type of the variable will be same as the data type of value.
3. What is a variable? Explain with example.
A variable is like a container that stores values. This value you can access and change.
Variable is used to provide a name to an object. Example:
X=150 (X here is a variable holding value 150)
4. Define a statement. Give one example also.
An instruction or a set of instruction written in any program is called statement.
Example:
print(“We are learning Python”)
5. What are comments? How are they useful in programming? How can we give a
comment in Python?
Comments are python statement that are ignored by Python interpreter.
Usefulness: Comments increase the readability of the code.
Comments in Python starts with hash symbol (#) and extends till the end of line.
Example:
# This is a comment
6. What is function? What is user-defined function in Python. Explain with an example.
How is it useful in Python?
Function is a named block of statements defined by the user to perform a specific task.
A user defined function can be created by def statement followed by a parentheses ()
along with parameters. A user-defined function is written in script mode and can be
called either in interactive or script mode itself.
Syntax:
def function_name (parameter1, parameter2):
In the above syntax:
def is a keyword of Python.
parameter1 and parameter2 are a variable that will hold the data to be processed
Example:
def rect_area(length, breadth):
Functions are useful as once defined it can be reused anywhere through function call.
We need not rewrite the same code every time it is needed.
7. What is the significance of indentation in Python?
Indentation is shifting of statement to the right of another statement.
Significance:
In Python indentation is used to define a block of statements. To define beginning and
end of a function, Python rely on indentation only. If you don’t give indentation to
define a block of statement/statements, it gives Indentation Error.
8. What are Tokens in Python? How many types of tokens are allowed in Python? Explain
with example.
Token – It is the smallest element/unit of Python program.
Token Types:
1. Identifiers - The name of any variable, constant, function, module
2. Literal - A fix numeric or non-numeric value
3. Operators - Symbols that perform some kind of operation (+, -, *, /)
4. Delimiters - Symbols which can be used as separators of values or to enclose
some values ({} , [] )
5. Keywords -These are reserved words in Python
Example:
Token Category
First_Name variable / identifier
X variable / identifier
print Keyword
= Operator / Delimiter
372 Literal
“Python” Literal
[1,2,3,4,5] [] Delimiter
9. What will be the output of give code block?
A=10
B=’Python’
print(A+B)
It will produce Type Error as variable A is holding integer type of data and variable B is
holding string type of data. And addition operator is applicable on same type of
operands.
[Link] the statement used to generate the output in Python.
Print statement is used to generate the output in Python.
11. Name different data types available in Python.
Data types in Python can be defined as mutable and immutable. A mutable data type
can be changed after it is created, and an immutable data type can't.
➢ Mutable
List
Dictionary
➢ Immutable
Integer
Float
String
Tuple
Boolean
12. Define String data type and the ways to define a string data in Python.
String data type represents sequence of characters. To define string type data
enclosed the values within quotation marks (single, double or triple). Example:
‘This is an example of string’
“10+60”
‘’’’Python_3.8’’
13. What happens if relational operators are applied on String data?
When relational operators are applied on String data, strings are compared left to
right character by character according to ASCII values.
14. What is the method to assign values to variables? Explain in detail.
Operator ‘=’ is used for assigning value to a variable. Value can be assigned to variable
in these different ways:
➢ Assigning same values to multiple variables
Jan, Mar, May, July, Aug, Oct, Dec = 31
➢ Assigning multiple values to multiple variables
Jan, Feb, Mar, April, May = 31, 28, 31, 30, 31
A variable is not created until some value is assigned to it
15. What are the naming rules for a variable in Python? Give some examples of valid
variables.
Variable Declaration Rules:
1. It must start either with alphabet (capital or small) or underscore ( _ )
2. It cannot start with a number
3. It can contain only alpha-numeric characters (A-Z, a-z, 0-9) and underscore ( _ )
4. Python keywords cannot be used as a variable name
5. It cannot contain spaces
6. Variable names are case-sensitive (name, Name and NAME are 3 different
variables)
Example (valid variable names):
First_Name
student15
_rollno
16. What is the difference between a keyword and a variable?
Keywords are the reserved words in Python. We cannot use a keyword as
a variable name, function name or any other identifier. They are used to define the
syntax and structure of the Python language. In Python, keywords are case sensitive.
For example – print, int, input, float
A variable is an identifier which is used to store a value. It is user-defined and are not
reserved. For example - First_Name, student15, _rollno etc.
17. How many types of strings are supported in Python?
➢ Single line string – String that terminates in single line
➢ Multiline string – String storing multiple lines of text
18. What is ‘None’ in Python?
‘None’ is special type in Python, which is used to indicate something that has not been
created yet. It represents not-null, unidentified value.
19. What is the difference between an expression and a statement?
Expression Statement
It is a combination of symbols, It is an instruction in Python following
operators & operands certain syntax
An expression represents some value Statement is given to execute a task
Expression always evaluate some result Statement may or may not evaluate the
result
Example: Example:
10**3+2-17 Print (“Python is easy”)
20. What do you understand by block/code block/suite in Python?
A group of statements that is part of another statement or function is called a
block/code block/suite. Example:
21. What would the following code do?
X = Y = Z = 55
X, Y and Z will be assigned with same i.e. 55
22. What are operators? What is their function? Mention different type of operators with
symbols available in Python.
Operators are special symbols which represents computation.
Operators Types
➢ Arithmetic - Solve arithmetic or algebraic expression
➢ Relational -These are comparison operators
➢ Logical - and , or , not
➢ Shorthand Assignment - A combination of binary and assignment operator
Operators Symbols
Arithmetic Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Remainder (%)
Exponent (**)
Integer Division (//)
Relational Less than (<)
Greater than (>)
Less than equal to (<=)
Greater than equal to (>=)
Not equal to (<>) , (!=)
Equal to (==)
Logical And
Or
Not
Shorthand Assignment +=
-=
*=
/=
**=