Python
Python
Python
Data Types
• Numeric - int, float, complex
• String - str
• Sequence - list, tuple, range
• Binary - bytes, bytearray, memoryview
• Mapping - dict
• Boolean - bool
• Set - set, frozenset
• None - NoneType
Python List Data Type
• A Python list contains items separated by commas and enclosed within
square brackets ([]).
Python Tuple Data Type
• Python tuple is another sequence data type that is similar to a list.
• A Python tuple consists of a number of values separated by commas.
• Unlike lists, however, tuples are enclosed within parentheses.
• The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their
elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be
updated.
• Tuples can be thought of as read-only lists.
Python Range
• Python range() is an in-built function in Python which returns a sequence of numbers starting
from 0 and increments to 1 until it reaches a specified number.
Python Dictionary
• Python dictionaries are kind of hash table type.
• They work like associative arrays or hashes found in Perl and consist of key-value pairs.
• A dictionary key can be almost any Python type, but are usually numbers or strings.
• Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces
([]).
Python Operator
• Python language supports the following types of operators.
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operator
Operator Name Example
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
<< Binary Left Shift Shift left by pushing zeros in from the right and let the leftmost
bits fall off
>> Binary Right Shift Shift right by pushing copies of the leftmost bit in from the left,
and let the rightmost bits fall off
Bitwise operator example
Python Loop
Sr.No. Loop Type & Description
1 while loopRepeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the loop
body.
3 nested loopsYou can use one or more loop inside any another while,
for or do..while loop.
Python While Loop
The Infinite loop
• A loop becomes infinite loop if a condition never becomes FALSE.
• Above example goes in an infinite loop and you need to use CTRL+C to exit the program.
Using else statement with while loop
• If the else statement is used with a while loop, the else statement is executed when the condition
becomes false.
Python for loop
For Loop Examples
Nested loop
• Python programming language allows to use one loop inside another loop.
• The following program uses a nested for loop to find the prime numbers from 2 to 100 −
Python Loop
Sr.No. Control Statement & Description
2 continue statementCauses the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.