Post

Python

Python:

Python is a high-level, interpreted programming language known for its readability, flexibility, and vast ecosystem of libraries. Designed with simplicity in mind, Python’s syntax is straightforward, making it an excellent choice for beginners and professionals alike. Created by Guido van Rossum in the late 1980s, Python has evolved into one of the most popular programming languages in the world.

Variables in Python

In Python, declaring a variable is as simple as assigning a value to a name using the = operator:

1
2
3
4
5
6
# Examples of variables
name = "Alice"        # string variable
age = 25              # integer variable
height = 5.7          # float variable
is_student = True     # boolean variable

Variable Naming Rules

  • Variable names must start with a letter (a-z, A-Z) or an underscore _.
  • Variable names cannot start with a number.
  • Only letters, numbers, and underscores are allowed in variable names.
  • Variable names are case-sensitive (age and Age would be two different variables).
  1. Examples of valid variable names:
1
2
3
first_name = "Alice"
_age = 30
height_in_cm = 175
  1. Examples of invalid variable names:
1
2
2nd_variable = "invalid"   # Starts with a number
first-name = "invalid"     # Contains a hyphen

Dynamic Typing

  • Integers (int): Whole numbers (e.g., 10, -5)
  • Floating Point Numbers (float): Numbers with decimals (e.g., 5.7, -3.14)
  • Strings (str): Text data (e.g., “hello”, “Python”)
  • Booleans (bool): Logical values True or False
  • Lists (list): Ordered, mutable collection of items (e.g., [1, 2, 3])
  • Tuples (tuple): Ordered, immutable collection of items (e.g., (1, 2, 3))
  • Dictionaries (dict): Key-value pairs (e.g., {“name”: “Alice”, “age”: 25})
1
2
3
4
5
6
7
8
x = 42
print(type(x))    # Output: <class 'int'>

y = 3.14
print(type(y))    # Output: <class 'float'>

z = "Python"
print(type(z))    # Output: <class 'str'>

Reassigning Variables

1
2
3
4
5
6
7
8
number = 10
print(number)     # Output: 10

number = 15       # Reassign with a new integer value
print(number)     # Output: 15

number = "ten"    # Reassign with a string value
print(number)     # Output: ten

Constants

Although Python does not have a built-in way to declare constants (variables that should not change), it’s a common convention to use all-uppercase names for variables intended to be constant:

1
2
PI = 3.14159
GRAVITY = 9.8

Multiple Variable Assignment

1
2
3
4
5
6
a, b, c = 1, 2, 3
print(a)    # Output: 1
print(b)    # Output: 2
print(c)    # Output: 3
x = y = z = 100
print(x, y, z)   # Output: 100 100 100

Variable Scope

  • Global Variables: Defined outside of a function and can be accessed anywhere in the code.
  • Local Variables: Defined inside a function and can only be accessed within that function.
1
2
3
4
5
6
7
8
9
10
global_var = "I am global"

def my_function():
    local_var = "I am local"
    print(global_var)    # This works
    print(local_var)     # This also works

my_function()
print(global_var)        # This works
# print(local_var)       # This would cause an error, as local_var is not accessible outside the function

Data

This post is licensed under CC BY 4.0 by the author.