Spex3
    

Basic Syntax and Data Types in Python 🐍


    

    

Python has a simple and readable syntax that makes it beginner-friendly. Let’s go over variables and data types in Python.

1️⃣ Variables in Python

A variable is used to store data in Python. You don’t need to specify the type of a variable—it is determined automatically.

Declaring Variables


name = "Alice" # String
age = 25 # Integer
height = 5.7 # Float
is_student = True # Boolean

✅ Rules for Naming Variables:

Must start with a letter or underscore (_).

Cannot start with a number.
Can contain letters, numbers, and underscores.
Case-sensitive (name and Name are different variables).

2️⃣ Data Types in Python

Python has several built-in data types. Here are the most common ones:

🔹 Integer (int)

Used for whole numbers.

x = 10
y = -5

print(type(x)) # Output:

🔹 Float (float)

Used for decimal numbers.


pi = 3.14
weight = 72.5
print(type(pi)) # Output:

🔹 String (str)

Used for text, enclosed in single (') or double (") quotes.

greeting = "Hello, World!"
char = 'A'
print(type(greeting)) # Output:

🔹 Multi-line strings use triple quotes (''' or """):


message = """This is
a multi-line string."""
print(message)
🔹 Boolean (bool)
Represents True or False values.

is_raining = False
has_license = True
print(type(is_raining)) # Output:

3️⃣ Type Conversion (Casting)

You can convert between data types using casting functions:

age = "25"
age_int = int(age) # Convert string to integer

pi = 3.14
pi_str = str(pi) # Convert float to string

is_happy = "True"
is_happy_bool = bool(is_happy) # Convert string to boolean

print(type(age_int)) # Output:
print(type(pi_str)) # Output:
print(type(is_happy_bool)) # Output:

4️⃣ Checking Data Types
Use the type() function to check the type of a variable.

x = 100
print(type(x)) # Output:

y = "Hello"
print(type(y)) # Output:


    Date: 2025-03-21 00:00:00.000000