TutorialsPythonPython Introduction
Share:

Python Introduction

beginner

Part of Python Basics

Theory

Python is a high-level, interpreted programming language created by Guido van Rossum and first released in 1991. It emphasizes code readability through significant whitespace and a clean syntax. Python follows a "batteries included" philosophy, offering a comprehensive standard library.

Why Python is popular:

  • Beginner-friendly — English-like syntax, minimal boilerplate
  • Versatile — web development (Django, Flask), data science (NumPy, pandas), AI/ML (TensorFlow, PyTorch), automation, scripting, game development
  • Large community — extensive documentation, packages, and support
  • Cross-platform — runs on Windows, macOS, Linux, and more

Python 2 vs Python 3: Python 3 (released 2008) is the current and future of Python. Python 2 was officially sunset in 2020. Always use Python 3 for new projects.

Running Python can be done in two ways:

  • Interactive shell — type python3 in the terminal to get a REPL (Read-Eval-Print Loop) for experimenting
  • Script files — save code in .py files and run with python3 filename.py

Basic syntax rules include:

  • Indentation matters — Python uses indentation to define code blocks, not curly braces. Consistent indentation (usually 4 spaces) is required.
  • Comments start with # for single-line and use triple quotes """ for multi-line docstrings
  • Statements typically end without semicolons (though they are allowed)

The print() function outputs text to the console. It can take multiple arguments and format strings.

Practical Examples

Example 1: Your First Python Program
python
Example 2: Python Syntax and Indentation
python

Exercises

Print a Poem

easy

Write a Python program that prints a short poem of at least 4 lines using multiple print() statements. Each line should be on a separate line in the output.

Starter Code:

# Write your poem here

Expected Output:

Roses are red,\nViolets are blue,\nPython is awesome,\nAnd so are you!

Personal Information Card

easy

Create variables for your name, age, and favorite hobby. Use print() to display them in a formatted card with a border made of asterisks.

Starter Code:

name = 'Alice'\nage = 25\nhobby = 'coding'\n# Print your info card here

Expected Output:

*****************\n* Name: Alice   *\n* Age: 25       *\n* Hobby: coding *\n*****************

Fix Indentation Errors

medium

The following code has indentation errors. Rewrite it correctly: \ndef greet():\nprint('Hello')\nif True:\nprint('World')\n print('!')\n

Starter Code:

# Fix the indentation

Expected Output:

Hello\nWorld\n!

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Simple Calculator Banner

Create a Python program that prints a decorative banner for a calculator application. The banner should display the program name, version, and a simple menu.

Requirements:

    Bonus Challenge

    Use escape characters (\t) for tab alignment in the menu.