Variables
beginnerPart 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 # boolNaming 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,0float— floating-point numbers:3.14,-0.5,1.0str— strings (immutable sequences of characters):"hello",'world'bool— boolean values:True,FalseNoneType— 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 floatstr(x)— converts to stringbool(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
Exercises
Personal Data Card
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 typesExpected Output:
Name: Alice <class 'str'>\nBirth Year: 1995 <class 'int'>\nHeight: 1.68 <class 'float'>\nPython Fun: True <class 'bool'>\nAge: 31Temperature Converter
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 displayExpected Output:
Enter temperature in Celsius: 25\n25.0°C = 77.0°FString Analyzer
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 sentenceExpected Output:
Character count: 23\nWord count: 4\nUppercase: I LOVE PYTHON!\nContains Python: TrueMini 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.