Python Fundamentals
Python Fundamentals
Fundamentals
• To understand Python fundamentals
Learning
Objectives
In Python, data types are the classification or
categorization of data items.
Data
types
Specifying Data Types
If you want to specify the data type, you can use constructor functions like :
Getting and Setting Data Types
You can get the data type of any object by using the type() function.
When you assign a value to a variable, the data type is set automatically.
For instance,
Tokens • Keywords
• Identifiers
• Literals
• Operators
• Punctuators
Keywords
They are used to specify the names of variables, functions, class, module, and objects.
When you type a program in IDLE some pieces of code are shown in different colours:
• A predefined function is shown in purple.
• A string is shown in green.
• Comments are shown in red.
• If you make a mistake you may spot the colours are wrong.
Keywords
IDLE Colour Coding
Literals
Literals can be defined as a data or value that is assigned to
a variable or a constant.
Numeric
There are three types of numeric literal:
Special
Letters Digits
Symbols Whitespace
A-Z, a-z 0-9
@!$
Strings
A string is literal and can be created by writing a text (a group of
characters ) surrounded by a single(”), double(“), or triple quotes.
We can write multi-line strings or display them in the desired way
by using triple quotes.
Boolean
There are only two Boolean literals in Python. They are true and false.
In Python, True represents the value as 1, and False represents the value as 0.
Special
Python contains one special literal (None).
‘None’ is used to define a null variable.
If ‘None’ is compared with anything else other than a ‘None’, it will return false.
Literal Collections
Python provides four different types of literal collections:
• List
• Tuple
• Dictionary
• Set
List
• The list contains items of different data types.
• The values stored in the List are separated by a comma (,) and enclosed within square brackets([]).
• We can store different types of data in a List. Hence list are heterogenous.
• Lists are mutable.
Tuple
• Tuple is a collection of different data-type.
• It is enclosed by the parentheses ‘()‘ and each element is separated by the comma(,).
• Tuples are immutable.
Dictionary
• The dictionary stores the data in the key-value pair. It is enclosed by curly braces ‘{}‘ and each pair is separated by
the commas(,).
• We can store different types of data in a dictionary.
• Dictionaries are mutable.
Set
• Set is the collection of the unordered data set.
• It is enclosed by the {} and each element is separated by the comma(,).
• Sets are mutable.
Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are
standard symbols used for logical and arithmetic operations. In this article, we will look into different types of
Python operators.
Programs • Comments
Blocks and Indentation
• Python uses indentation to define blocks of code, unlike other languages that use braces.
• This means that the whitespace at the beginning of the line is significant and must be consistent.
• Blocks of code are denoted by line indentation, which is rigidly enforced.
• The number of spaces in the indentation is variable, but all statements within the block must be indented the same
amount.
Statements
Python statements can be broadly categorized into simple and compound statements:
Simple Statements: These are the most basic kind of statement that does something. These are single line
statements. Examples include expression statements, assignment statements, and the pass, del, return, import,
continue, and break statements.
Compound Statements: These contain groups of other statements and control the execution flow of the program.
Examples include if, while, for, try, and with statements.
Comments
• Comments are not executed.
• Comments in Python start with the # symbol and are used to explain what the code is doing.
• All characters after the # and up to the end of the physical line are part of the comment and the Python
interpreter ignores them.
• They're crucial for making your code understandable to others and to your future self.
• There are two types of comments in python:
Single line comment
Multi-line comment
Comments Contd..
Single line comment: This type of comments start in a line and when a line ends, it is automatically ends.
Single line comment starts with # symbol.
Multi-Line comment: Multiline comments can be written in more than one lines. Triple quoted ‘ ’ ’ or “ ” ”) multi-
line comments may be used in python. It is also known as docstring.
Example:
‘’’ This program will calculate the average of 10 values.
First find the sum of 10 values
and divide the sum by number of values
‘’’
Punctuators
ns Expression
s