Share:

Variables

beginner

Part of Python Basics

Theory

Variables in Python are containers for storing data. Unlike many other languages, Python uses dynamic typing — you don't need to declare the type of a variable explicitly. The interpreter infers the type at runtime.

name = "Alice"      # string
age = 25            # int
height = 5.6        # float
is_student = True   # bool

Naming conventions: Variable names should be descriptive, use lowercase with underscores (snake_case), start with a letter or underscore, and cannot be Python keywords.

Python has several basic data types:

  • int — integers (unlimited precision): 42, -10, 0
  • float — floating-point numbers: 3.14, -0.5, 1.0
  • str — strings (immutable sequences of characters): "hello", 'world'
  • bool — boolean values: True, False
  • NoneType — represents absence of value: None

The type() function returns the type of any value. This is useful for debugging and understanding your data.

Type conversion converts between types:

  • int(x) — converts to integer (truncates float, parses string)
  • float(x) — converts to float
  • str(x) — converts to string
  • bool(x) — converts to boolean

f-strings (formatted string literals) provide a concise way to embed expressions in strings. Prefix a string with f or F and use {expression} to embed values.

name = "Bob"
age = 30
print(f"{name} is {age} years old.")

String operations include concatenation (+), repetition (*), slicing ([start:stop:step]), and methods like upper(), lower(), strip(), split(), join(), find(), replace().

The input() function reads user input from the keyboard as a string. Always convert to the required type using int() or float() when performing numeric operations.

Practical Examples

Example 1: Variables, Types, and Type Conversion
python
Example 2: f-strings and String Operations
python

Exercises

Personal Data Card

easy

Create variables for your name (string), birth_year (int), height in meters (float), and is_python_fun (bool). Print each variable with its type using type(). Then calculate and print your approximate age.

Starter Code:

# Create your variables and print them with their types

Expected Output:

Name: Alice <class 'str'>\nBirth Year: 1995 <class 'int'>\nHeight: 1.68 <class 'float'>\nPython Fun: True <class 'bool'>\nAge: 31

Temperature Converter

easy

Use input() to get a temperature in Celsius from the user. Convert it to Fahrenheit using F = C * 9/5 + 32. Use an f-string to display the result formatted to 1 decimal place.

Starter Code:

celsius = float(input('Enter temperature in Celsius: '))\n# Convert and display

Expected Output:

Enter temperature in Celsius: 25\n25.0°C = 77.0°F

String Analyzer

medium

Take a sentence as input using input(). Count and print: the number of characters (including spaces), the number of words (split by spaces), the uppercase version, and whether it contains the word 'Python'.

Starter Code:

sentence = input('Enter a sentence: ')\n# Analyze the sentence

Expected Output:

Character count: 23\nWord count: 4\nUppercase: I LOVE PYTHON!\nContains Python: True

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Interactive User Profile

Create a program that asks the user for their details using input(), performs type conversions, and prints a formatted profile summary.

Requirements:

    Bonus Challenge

    Ask for multiple favorite colors, store them in a list using split(), and print them.