Variables and Data Types

Variables and Data Types

What is a Variable?

A variable is a container for storing data values. In Python, you don't need to declare the type.

Creating Variables

name = "John"      # String
age = 25           # Integer
height = 1.75      # Float
is_student = True  # Boolean

Basic Data Types

Strings (str)

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

String methods

print(message.upper()) # HELLO, WORLD! print(len(message)) # 13

Integers (int)

count = 42
negative = -10

Floats (float)

price = 19.99
temperature = -5.5

Booleans (bool)

is_active = True
has_permission = False

Type Checking

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

y = "Hello" print(type(y)) # <class 'str'>

Type Conversion

# String to Integer
num = int("42")    # 42

Integer to String

text = str(42) # "42"

String to Float

price = float("19.99") # 19.99

Summary

  • Variables store data
  • Python has dynamic typing
  • Common types: str, int, float, bool

Next Lesson

In the next lesson, you'll learn about operators in Python.

Quiz - Quiz - Variables and Data Types

1. What is a variable in Python?

2. What is the data type of 3.14?

3. How do you convert a string to an integer?

Installation and Setup