First Python Program
First Python Program
Section titled “First Python Program”Introduction
Section titled “Introduction”The tradition is “Hello, World!” — the simplest program that verifies your environment works.
Hello, World!
Section titled “Hello, World!”print("Hello, World!")Output:
Hello, World!A More Meaningful First Program
Section titled “A More Meaningful First Program”name = input("What's your name? ")age = int(input("How old are you? "))print(f"\nHello, {name}!")print(f"In 10 years, you'll be {age + 10} years old.")print() Function Deep Dive
Section titled “print() Function Deep Dive”print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
# sep — separatorprint("a", "b", "c", sep="-") # a-b-c
# end — what to print at endprint("Hello", end="") # No newline
# file — output to filewith open("output.txt", "w") as f: print("To file", file=f)Why It Matters
Section titled “Why It Matters”Your first program establishes that your development environment is correctly set up, and introduces the most basic input/output operations.
Interview Questions
Section titled “Interview Questions”Q1: What does print("a", "b", "c", sep="-") output?
A: a-b-c. The sep parameter controls how multiple arguments are separated.
Q2: How do you print without a newline?
A: print("text", end="") — set the end parameter to an empty string.
Practice Exercises
Section titled “Practice Exercises”- Print your name, age, and city using a single print statement.
- Create a program that asks for your favorite color and prints a message.
- Use
print()with differentsepandendcombinations.