DataTypes Flashcards
Data types in Python
Data Types
Data types in Python define the type of value a variable can hold, such as numbers, text, or more complex structures.
Basic Data Type
Basic Data Type
- Numeric Types: int, float, complex
- Text Type: str
- Boolean Type: bool
- None Type: Represents a null or undefined value (None)
Sequence Types
- list: Ordered, mutable collection.
- tuple: Ordered, immutable collection.
- range: Sequence of numbers.
Mapping Type
- dict: Key-value pairs.
Set Types
- set: Unordered, unique elements.
- frozenset: Immutable version of a set.
Special Data Types
- bytes: Immutable sequence of bytes.
- bytearray: Mutable sequence of bytes.
Operators
Operators in Python are symbols or keywords used to perform operations on variables and values, such as arithmetic, comparison, or logical evaluations.
Types of Operators
Arithmetic Operators
+ - * /
% Modulus: 5 % 3 = 2
** Exponentiation: 5 ** 3 = 125
// Floor Division: 5 // 3 = 1
Comparison Operators
== != > < >= <=
Logical Operators
and or not
Bitwise Operators
& AND: 5 & 3 # 1
^ XOR: 5 ^ 3 # 6
~ NOT: ~5 # -6
«_space; Left shift: 5 «_space;1 # 10
» Right shift:5»_space; 1 # 2
Assignment Operators
= Assign: x = 5
+= Add and assign: x += 3 # x = x + 3
-= Subtract and assign: x -= 3
*= Multiply and assign: x *= 3
/= Divide and assign: x /= 3
%= Modulus and assign: x %= 3
Membership Operators
in: 3 in [1, 2, 3] # True
not in: 4 not in [1, 2, 3] # True
Identity Operators
is: x is y
is not: x is not y
OR: 5 | 3 # 7
Conditional Statement
In Python, conditional statements are used to execute a block of code based on certain conditions.
The most common conditional statements are if, elif, and else
Loops in Python
Loops in Python are used to repeatedly execute a block of code as long as a specified condition is met.
There are two main types of loops in Python: for, while
for variable in sequence:
# Code block to execute
while condition:
# Code block to execute
Control Statements in Loops
- break:
Exits the loop prematurely.
for i in range(5):
if i == 3:
break
print(i)
Output: 0, 1, 2 - continue:
Skips the rest of the loop’s current iteration.
for i in range(5):
if i == 3:
continue
print(i)
Output: 0, 1, 2, 4 - else with Loops:
Executes if the loop completes normally (without break).
for i in range(5):
print(i)
else:
print(“Loop completed”)
Output: 0, 1, 2, 3, 4, Loop completed
Data Types vs Data Structures
Data Types
Represent a single value
Simple and atomic.
Purpose: To store one piece of data.
Examples: int, float, str, bool.
Operations: Basic (e.g., arithmetic, concatenation).
Data Structures
Organize and manage multiple values.
Complex and composite.
Purpose: To handle collections of data efficiently.
Examples: list, tuple, set, dictionary.
Operations: Advanced (e.g., indexing, slicing, sorting).
Inbuilt data structures in Python
List - A list is an ordered, mutable collection that can store elements of different data types.
Tuple- A tuple is an ordered, immutable collection of elements.
Set- A set is an unordered collection of unique elements.
Dictionary- A dictionary is an unordered collection of key-value pairs.