Python Fundamentals: Variables, Data Types, and Operators

Variables

Variables serve as containers for storing data values in memory.

Syntax: variable_name = value

Key characteristics:

  • No explicit type declaration required
  • Type is automatically determined from assigned value
  • Variables must be assigned before use
# Variable definition and usage
value = 20
print(value)

Data Types

Python supports several built-in data types:

Core Data Types

  • Numeric Types: int, float, complex, bool
  • String: Ordered character sequences enclosed in quotes
  • List: Mutable ordered collection using []
  • Tuple: Immutable ordered collection using ()
  • Set: Unordered unique collection using {}
  • Dictionary: Key-value pairs using {}

Type Examples

# Numeric types
integer_val = 42
float_val = 3.14159
complex_val = 2+3j
bool_val = True

# String types
single_quoted = 'text'
double_quoted = "text"
triple_quoted = """multiline
text"""

# Collection types
sample_list = [1, 'item', 3.14]
sample_tuple = (5, 'hello')
sample_set = {'apple', 42, 'banana'}
sample_dict = {"name": "John", "age": 25}

Type Inspection

# Check variable type
person = {"name": "Alice", "age": 30}
print(type(person))  # Output: <class 'dict'>

Numeric Data Types

Integer Type (int)

Integers support multiple base representations:

# Different integer representations
decimal_val = 15
binary_val = 0b1111    # Binary
octal_val = 0o17       # Octal  
hex_val = 0xF          # Hexadecimal

Base Conversion Functions

Function Description
bin(x) Convert to binary
oct(x) Convert to octal
int(x) Convert to decimal
hex(x) Convert to hexadecimal
# Base conversion examples
num = 18
print(bin(num))    # 0b10010
print(oct(num))    # 0o22
print(hex(num))    # 0x12

Floating-Point Type (float)

Represents decimal numbers:

pi = 3.14159
scientific = 2.5e-3  # 0.0025

Complex Type (complex)

comp_num = 4 + 7j
print(comp_num.real)  # 4.0
print(comp_num.imag)  # 7.0

Boolean Type (bool)

Special integer subtype where True = 1 and False = 0

Falsy values in Python:

  • None
  • False
  • Zero values: 0, 0.0, 0j
  • Empty sequences: "", (), []
  • Empty dictionaries: {}
print(bool(""))     # False
print(bool(0))      # False  
print(bool("text")) # True

Type Conversion

Function Description
int(x) Convert to integer
float(x) Convert to float
complex(x) Convert to complex
# Type conversion examples
float_num = 7.8
print(int(float_num))    # 7
print(float(10))         # 10.0
print(complex(5))        # (5+0j)

Input and Output

Input Function

user_input = input("Enter value: ")
# Returns string type

Print Function

print("Hello", "World", sep="-", end="!")
# Output: Hello-World!

Operators

Arithmetic Operators

Operator Description Example
+ Addition 5 + 3 → 8
- Subtraction 5 - 3 → 2
* Multiplication 5 * 3 → 15
/ Division 5 / 2 → 2.5
% Modulus 5 % 2 → 1
** Exponentiation 5 ** 2 → 25
// Floor Division 5 // 2 → 2

Comparison Operators

Operator Description Example
== Equal 5 == 3 → False
!= Not equal 5 != 3 → True
> Greater than 5 > 3 → True
< Less than 5 < 3 → False
>= Greater or equal 5 >= 3 → True
<= Less or equal 5 <= 3 → False

Assignment Operators

Operator Example Equivalent
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
**= x **= 3 x = x ** 3
//= x //= 3 x = x // 3

Bitwise Operators

Assume: a = 60 (binary: 0011 1100), b = 13 (binary: 0000 1101)

Operator Description Result
& AND a & b → 12 (0000 1100)
| OR a | b → 61 (0011 1101)
^ XOR a ^ b → 49 (0011 0001)
~ NOT ~a → -61 (1100 0011)
<< Left shift a << 2 → 240 (1111 0000)
>> Right shift a >> 2 → 15 (0000 1111)

Tags: python programming Variables data-types Operators

Posted on Fri, 11 Sep 2026 16:34:32 +0000 by geoffjb