Introduction to Python
Discover Python, understand how it executes your code, explore real-world applications, and write your first Python programs.
In this lesson, you will learn
- ✓Define a programming language and explain why Python is considered a high-level, general-purpose language.
- ✓Identify Guido van Rossum as the creator of Python and explain Python’s emphasis on readability.
- ✓Describe practical applications of Python in web development, automation, data science, artificial intelligence, and software testing.
- ✓Distinguish Python source code, bytecode, the Python virtual machine, and the role of the Python interpreter.
- ✓Write, execute, and trace a simple Python program using print() and basic expressions.
- ✓Explain the difference between writing source code and executing a program.
1. What Is a Programming Language?
A computer follows instructions represented in forms that its hardware can execute. A programming language gives developers a structured way to express those instructions and describe the behavior they want from a computer.
For example, a program can instruct a computer to display a message, calculate a total, store information, or repeat a task. Programming languages provide rules for expressing these instructions. These rules include syntax, which describes how valid code must be written, and semantics, which describes what valid code means.
Python is one such language. It lets you express operations using readable statements instead of manually writing the machine instructions for every operation.
Your first Python instruction
print() is a built-in Python function that displays information. The text inside quotation marks is a string, which is a sequence of characters. The parentheses contain the argument passed to print(). When the program executes, Python displays the text without the quotation marks.
Note
2. What Is Python?
Python is a high-level, general-purpose programming language. Each of these terms describes an important characteristic.
High-level means Python provides abstractions that let you work with concepts such as strings, lists, dictionaries, and functions without manually managing every low-level machine operation. You still need to understand how your program uses memory and other resources, but Python handles many implementation details for you.
General-purpose means Python is not restricted to a single category of work. The same language can be used for web services, automation scripts, data analysis, scientific computing, testing, and many other tasks.
Python emphasizes readable syntax. Indentation defines code blocks in many constructs, and its syntax often allows a solution to be expressed with fewer lines than an equivalent low-level implementation.
Python is dynamically typed: you do not normally declare a variable’s type before assigning a value. Types belong to objects, and a variable name can be rebound to objects of different types during execution.
Python code is designed to be readable
The first statement binds the name 'name' to a string object. The second statement binds the name age to an integer object. Each print() call displays the value associated with its argument. Python does not require declarations such as string name or int age. The example illustrates ordinary variable assignment, not a requirement that every variable retain the same type forever.
Note
3. Who Created Python?
Python was created by Guido van Rossum. Work on the language began in the late 1980s, and Python was first publicly released in 1991.
Python was designed with an emphasis on readability, simplicity, and making code easier to understand. These characteristics make it approachable for beginners and productive for experienced developers.
Python is maintained and developed by an open-source community. The Python Software Foundation supports the Python language and its ecosystem. The official Python documentation is the primary reference for learning language features and standard-library functionality.
Readable code communicates intent
first_name is a descriptive variable name. The plus operator concatenates the two strings. The resulting string is passed to print(). Descriptive names help readers understand the purpose of a statement without needing a separate explanation.
Note
4. Where Is Python Used?
Python is used across many industries because it has a readable syntax, a large standard library, and a broad ecosystem of third-party packages. The most suitable tool depends on the problem being solved.
Web development: Frameworks and libraries such as Django, Flask, and FastAPI help developers build websites, APIs, and backend services.
Automation and scripting: Python can rename files, process documents, organize folders, call APIs, generate reports, and automate repetitive tasks.
Data science and analytics: Libraries such as NumPy, pandas, and Matplotlib support numerical computation, data manipulation, and visualization.
Artificial intelligence and machine learning: Libraries such as PyTorch and scikit-learn support model development, experimentation, and machine-learning workflows.
Testing and quality assurance: Python can automate test cases, verify API responses, test application behavior, and support continuous integration.
Scientific computing and education: Python is used for numerical experiments, research workflows, teaching programming, and prototyping ideas.
Python is not necessarily the best choice for every task. Some applications require specialized hardware control, strict timing guarantees, or performance characteristics that may favor other languages or a combination of languages.
A small automation-style task
The list stores three filenames. The for loop visits each element in the list, in order. During each iteration, file_name refers to the current filename. print() displays a label and the filename. This illustrates the type of repeated operation commonly used in automation. It prints filenames; it does not actually open or modify files.
Note
5. Writing and Running Python Code
Writing a program and executing it are two different activities. Writing means creating source code. Executing means asking a Python implementation to run that code.
Python source files commonly use the .py extension. You can create a file such as hello.py in a text editor or an integrated development environment (IDE).
To run a script from a terminal, use python hello.py on systems where python invokes the intended Python interpreter. On many Linux and macOS installations, and on some Windows setups, python3 hello.py is the appropriate command. The exact command depends on how Python is installed and configured.
You can also use an interactive Python shell, which lets you enter expressions and see their results immediately. A script is useful for saving and rerunning a complete program, while an interactive shell is convenient for experiments.
A code editor helps you write and organize source code. The Python interpreter or runtime executes the program. An IDE may combine editing, execution, debugging, and other development tools in one environment.
A complete Python script
The comment beginning with # describes the file and is not executed as a Python statement. The assignment binds message to a string. The print() statement displays the string. Save these statements in hello.py to create a small script. Running the script executes the statements in order and displays the output.
Note
6. How Python Executes Your Code
Understanding Python execution helps explain why Python is often described as an interpreted language, even though common Python implementations use an intermediate bytecode representation.
Consider CPython, the standard and most widely used Python implementation. In a typical execution of a .py file, CPython parses the source code, compiles it into Python bytecode, and evaluates that bytecode using its runtime machinery, including the Python virtual machine.
Source code is the human-readable program you write. Bytecode is a lower-level, implementation-specific representation of instructions. The Python virtual machine is the part of the runtime that evaluates Python bytecode.
Python may cache compiled bytecode in .pyc files, commonly within __pycache__, when applicable. The existence of such a file is an implementation detail and is not a requirement for every Python program to run.
The Python virtual machine is not the same thing as the physical CPU. The virtual machine evaluates Python bytecode using the underlying implementation and operating system. The processor ultimately executes machine instructions generated or used by that implementation.
Python has multiple implementations, and their internal execution strategies differ. Therefore, the source-code-to-bytecode explanation is a useful model for CPython, not a universal guarantee about every Python implementation.
Source code that produces bytecode when compiled
The source code contains an assignment and a function call. In CPython, the source is compiled into Python bytecode as part of execution. The runtime evaluates the instructions that implement the assignment, multiplication, and print() call. The program displays 10. You do not need to create or inspect bytecode manually to run this program.
Note
7. Python Syntax and Basic Rules
Syntax is the set of rules that determines whether a program is written in a valid form. Python code must follow these rules before it can execute successfully.
Indentation is significant in Python. It defines the body of constructs such as if statements, loops, functions, and classes. Four spaces per indentation level is the conventional style.
Strings are commonly written using single or double quotation marks. Both are valid for ordinary string literals, provided the opening and closing delimiters match.
Comments begin with # outside a string. Python ignores the comment text when executing the program.
Python is case-sensitive. For example, score and Score are different names.
A SyntaxError indicates that Python cannot parse the code as valid syntax. Other errors can occur after parsing, including exceptions raised during execution.
Indentation defines a code block
The condition score >= 50 evaluates to True. The indented print() statement belongs to the if block and runs. The final print() statement is outside the block and runs afterward. If the score were less than 50, the indented statement would be skipped, but the final print() would still execute.
Note
8. Common Beginner Errors
Learning to program includes learning to recognize errors. An error message is useful evidence about what went wrong, not a sign that you cannot learn programming.
A missing or mismatched quotation mark can cause a SyntaxError. Misspelling a name can cause a NameError if that name has not been defined. Using an unsupported operation between incompatible values can raise a TypeError.
Python also distinguishes between valid syntax and valid program behavior. A program can execute without errors and still produce the wrong result because its logic does not match the requirement.
Read error messages from the traceback, identify the relevant line, inspect the names and values involved, and reproduce the issue with the smallest example possible.
A NameError caused by a misspelled name
The assignment creates the name language. The print() call attempts to look up languge, which is a different name. Because languge has not been defined, Python raises a NameError. Correct the spelling to print(language). The exact traceback wording can vary by Python version and execution environment; the important fact is that the referenced name is undefined.
Note
Common pitfalls to avoid
Real debugging lessons and exact syntax contrasts to avoid runtime bugs
#1 Thinking Python is only used by beginners or only for web development.
Why it happens: This misconception comes from seeing Python used in beginner tutorials and web projects, which can hide its broader uses in automation, data analysis, testing, and AI.
Key takeaway: Python is a general-purpose language used in automation, web services, analytics, scientific computing, testing, and AI. Its suitability depends on the task.
#2 Assuming that a high-level language does not use machine instructions or a runtime.
Why it happens: High-level syntax hides many low-level details, so it is easy to overlook that the runtime ultimately relies on the operating system and processor.
Key takeaway: Python provides high-level abstractions, but an implementation still needs a way to execute its program on the underlying system.
#3 Claiming that Python source code always executes directly without compilation.
Why it happens: The phrase “interpreted language” can make execution sound like source text is run directly; CPython normally compiles source to bytecode first.
Key takeaway: In CPython, source code is typically compiled to bytecode, which the runtime evaluates. Other implementations may use different execution strategies.
#4 Believing that the Python virtual machine is the same as the computer’s physical CPU.
Why it happens: The word “virtual machine” sounds like a physical processor, but Python's virtual machine is a software component of the runtime.
Key takeaway: The Python virtual machine is part of the Python runtime and evaluates Python bytecode. The physical CPU executes machine instructions.
#5 Assuming variable names have fixed declared types in Python.
Why it happens: Many languages require declared variable types, so learners may incorrectly transfer that expectation to Python's dynamically typed name bindings.
Key takeaway: Python is dynamically typed. Names can be rebound to objects of different types, while the objects themselves retain their types.
#6 Using inconsistent indentation or mismatched quotation marks.
Why it happens: Beginners may be unfamiliar with Python's indentation-based blocks and quote delimiters, which makes inconsistent spacing or mismatched quotes easy to introduce.
Key takeaway: Use consistent indentation, conventionally four spaces per level, and make sure string delimiters match.
#7 Assuming that a program is correct just because it runs without an exception.
Why it happens: A program can run without crashing and still calculate or display the wrong result; checking expected output and edge cases catches logical errors.
Key takeaway: Compare actual output with expected output, trace the program, and test representative inputs and edge cases.
Ready for hands-on challenges?
Test your skills with shuffled coding challenges in the Practice Arena.
Technical and placement interview questions
Frequently tested interview questions with detailed, full answers (click to expand)
Q1:What is Python?
Q2:Who created Python, and when was it first released?
Q3:Why is Python called a high-level language?
Q4:What does general-purpose mean?
Q5:How does CPython execute a Python program?
Q6:What is the difference between source code and bytecode?
Q7:Is Python interpreted or compiled?
Q8:What does dynamically typed mean in Python?
Q9:What is the difference between a syntax error and a runtime exception?
Q10:Why is indentation important in Python?
Key takeaways
- Python is a high-level, general-purpose programming language designed with readability in mind.
- Guido van Rossum created Python, and its first public release was in 1991.
- Python is used in web development, automation, data science, AI, testing, and scientific computing.
- Writing source code and executing it are separate activities. Python scripts commonly use the .py extension.
- In typical CPython execution, source code is compiled into bytecode that the runtime evaluates.
- The Python virtual machine is part of the runtime, not the physical CPU.
- Python is dynamically typed and case-sensitive.
- Indentation, consistent naming, exact output checking, and careful debugging are fundamental Python skills.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: