Skip to content

Latest commit

Β 

History

History
216 lines (151 loc) Β· 5.54 KB

variables-and-data-types.md

File metadata and controls

216 lines (151 loc) Β· 5.54 KB

πŸ“˜ Variables and Data Types

πŸ“¦ Variables

What is a Variable?

Variables in Python are used to store data values. Unlike many other programming languages, Python does not require you to declare a variable’s type explicitly. A variable is created the moment you assign a value to it.

x = 5 # x is a nickname for the memory location where 5 is stored

Dynamic Typing

Python variables are dynamically typed, meaning you can reassign a variable to a different data type.

x = 5 # x is an integer
x = "Hello, World!" # x is a string

Under the hood, Python uses pointers to reference memory locations where data is stored. When you assign a variable to a value, you are essentially creating a pointer to the memory location where the value is stored.

Naming Conventions and Best Practices

In Python, variable names should follow specific rules:

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_), followed by letters, numbers, or underscores.
  • Variable names are case-sensitive (var and Var are different).
  • Avoid using Python keywords as variable names (class, for, if, etc.).

Good practices:

  • Use meaningful variable names (num_students instead of n).
  • Use snake_case (all lowercase with underscores) for variable names (student_name).

🏷️ Constants

Although Python does not have a true constant type, the convention is to define constants using all-uppercase names:

PI = 3.14159
PYTHON_VERSION = 3

Other naming conventions will be covered in the Style Guide section.

πŸ”’ Basic Data Types

Integers(int)

Integers are whole numbers without a decimal point. They can be positive or negative. They have unlimited precision.

x = 5
y = -10

Floats(float)

Floating-point numbers are real numbers with a decimal point. They are used to represent values with fractional components.

pi = 3.14159
height = 5.8

Strings(str)

Strings are sequences of characters enclosed in single (') or double (") quotes.

name = "Alice"
message = 'Hello, World!'

Booleans(bool)

Booleans represent one of two values: True or False.

u_r_handsome = False
i_love_python = True

🧱 Compound Data Types

Python supports several compound data types. Each will be covered in detail in later sections. Here, we provide a brief overview.

Lists(list)

  • Ordered collection of items.
  • Any data type can be stored.
  • Mutable (can be changed).
fruits = ["apple", "banana", "cherry"]

Tuples(tuple)

  • Almost identical to lists.
  • BUT, immutable (cannot be changed).
coordinates = (5, 10, 10.3)

Dictionaries(dict)

  • Unordered collection of key-value pairs.
  • Mutable (can be changed).
person = {
    "name": "Jun",
    "age": 29,
    "city": "Seoul"
}

Sets(set)

  • Unordered collection of unique items.
  • Mutable (can be changed).
unique_numbers = {1, 2, 3, 4, 5}

πŸ“ Type Checking and Conversion

Python allows you to check the type of a variable using the type() function and convert between different data types.

type() function

To check the type of a variable:

x = 5
print(type(x)) # <class 'int'>

Type Conversion

You can convert between types using functions like int(), str(), float(), etc.

x = 5
y = str(x) # "5"

z = 3.14
w = int(z) # 3, truncates the decimal part

# when it fails, it raises a ValueError
invalid_number = "Hello, World!"
int(invalid_number) # ValueError raised -> will be stopped here

More on type conversion in the Type Conversion section.

πŸ”— Mutability and Immutability

  • Mutable Data Types: Lists, Dictionaries, Sets
  • Immutable Data Types: Integers, Floats, Strings, Tuples

Under the hood: Mutable data types can be changed after they are created. When you modify a mutable object, Python changes the object in memory. They are passed by reference. Immutable data types cannot be changed after they are created. When you modify an immutable object, Python creates a new object in memory.

# Immutable
x = 5
print(id(x)) # 140732674004048

x = 10
print(id(x)) # 140732674004208: Different memory location

# Mutable
y = [1, 2, 3]
print(id(y)) # 140732674004048

y.append(4)
print(id(y)) # 140732674004048: Same memory location

⬅️ Previous: Hello, World!

➑️ Next: Basic Syntax and Comments

πŸ” Back to Top

🏠 Home