DataTypes Flashcards

Data types in Python

1
Q

Data Types

A

Data types in Python define the type of value a variable can hold, such as numbers, text, or more complex structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Basic Data Type

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Operators

A

Operators in Python are symbols or keywords used to perform operations on variables and values, such as arithmetic, comparison, or logical evaluations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Types of Operators

A

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
&laquo_space; Left shift: 5 &laquo_space;1 # 10
» Right shift:5&raquo_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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Conditional Statement

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Loops in Python

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Control Statements in Loops

A
  1. break:
    Exits the loop prematurely.
    for i in range(5):
    if i == 3:
    break
    print(i)
    Output: 0, 1, 2
  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
  3. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Data Types vs Data Structures

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Inbuilt data structures in Python

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly