Getting Started with Python
Introduction
Python is an interpreted, dynamically typed language, which means you can experiment with code interactively before committing it to a file. The Python interpreter reads a line of code, runs it immediately, and shows you the result — this is a great way to explore the language before writing full scripts (known in Python as modules).
Every Python script can read input from the keyboard using the input() function, and write output using the print() function. Both functions look deceptively simple, but there's some depth here: input() always returns a str, even if the user types a number, so you often need to convert it using functions like int() or float(). Likewise, print() supports several ways of building up an output string, including simple concatenation with +, C-style format specifiers with %, and (in modern Python) f-strings.
This chapter walks through those fundamentals: creating variables, printing values, converting between types, and formatting output.
Concepts
Variables and print()
name = "John Smith"
age = 21
print(name)
print(age)
Concatenation requires matching types
# This raises a TypeError - can't concatenate str and int directly
# print(name + " is " + age + " years old")
# Use str() to convert first
print(name + " is " + str(age) + " years old")
% style formatting
print("%s is %d years old" % (name, age))
Common format specifiers:
%s— string%d— decimal integer%f— floating point%x— hexadecimal
Escape sequences
\nnewline\ttab\rcarriage return\\backslash\"double quote
input()
user_name = input("Enter your name: ") # always returns a str
user_age = input("Enter your age: ")
user_age = int(user_age) # convert to int before doing maths
Multi-line strings
message = """
=========================
= FORMATTED INFORMATION =
=========================
"""
print(message)
Examples
name = "John Smith"
age = 21
print(name)
print(age)
print(name + " is " + str(age) + " years old")
print("%s is %d years old" % (name, age))
# Escape sequences
print("Line one\nLine two")
print("Name:\tJohn Smith")
Challenges
1. Print your details
- Create variables to hold your name and age (hard-coded is fine).
- Print each variable on its own line using
print(). - Print both values combined into a single sentence, converting the age to a string where needed.
💡 Show Answer
name = "Alice Kelly"
age = 27
print(name)
print(age)
print(name + " is " + str(age) + " years old")
2. Format specifiers
- Use the
%operator to print your name and age using%sand%dformat specifiers. - Try a few other format specifiers (e.g.
%ffor a floating point number) to see what output they produce.
💡 Show Answer
name = "Alice Kelly"
age = 27
height = 5.6
print("%s is %d years old" % (name, age))
print("%s is %.1f feet tall" % (name, height))
3. Escape sequences
- Write a print statement that uses
\n,\t, and\\to lay out some information in a readable block.
💡 Show Answer
print("Name:\tAlice Kelly\nAge:\t27\nPath:\tC:\\Users\\alice")
4. userinfo.py
- Write a script that asks the user to enter their name and age.
- Calculate the user's age next birthday.
- Output the details in a clear sentence.
💡 Show Answer
name = input("Enter your name: ")
age = int(input("Enter your age: "))
age_next_birthday = age + 1
print(name + " will be " + str(age_next_birthday) + " next birthday.")
5. Formatted block with a multi-line string
- Using a triple-quoted string, produce a formatted block of information similar to the following, substituting in a name and age:
=========================
= FORMATTED INFORMATION =
=========================
Name: Andy
You'll be 22 soon!
=========================
💡 Show Answer
name = "Andy"
age_next_birthday = 22
message = f"""=========================
= FORMATTED INFORMATION =
=========================
Name: {name}
You'll be {age_next_birthday} soon!
========================="""
print(message)
6. String concatenation practice
- Experiment with adjacent string literal concatenation (Python automatically joins strings written next to each other) as well as the
+operator.
💡 Show Answer
# Adjacent string literals are concatenated automatically
greeting = "Hello, " "world!"
print(greeting)
# The + operator concatenates variables and literals
name = "Alice"
greeting2 = "Hello, " + name + "!"
print(greeting2)