Skip to content
PythonPython Foundationsbeginner25 min read

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

Your first Python instruction
python
print('Hello, CodeNivo!')
Output
Hello, CodeNivo!

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

A program is a collection of instructions. A programming language defines how those instructions are expressed and what they mean. Python is the language; your Python program is the set of instructions you write using that language.

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

Python code is designed to be readable
python
name = 'Satyam'
age = 22
print(name)
print(age)
Output
Satyam 22

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

High-level does not mean that Python runs without an interpreter or that it is always faster to execute than a compiled language. Readability, abstraction, execution model, and performance are different concepts.

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

Readable code communicates intent
python
first_name = 'Satyam'
print('Welcome, ' + first_name)
Output
Welcome, Satyam

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

Remember the historical facts for interviews: Guido van Rossum created Python, and its first public release was in 1991.

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

A small automation-style task
python
files = ['report.txt', 'notes.txt', 'data.csv']
for file_name in files:
print('Processing:', file_name)
Output
Processing: report.txt Processing: notes.txt Processing: data.csv

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

Learning the language fundamentals comes before mastering individual frameworks. Variables, conditions, loops, functions, collections, and error handling transfer across Python application areas.

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

A complete Python script
python
# hello.py
message = 'Hello from Python'
print(message)
Output
Hello from Python

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

If a command is not recognized, Python may not be installed or its executable may not be on your PATH. Check your installation and use the interpreter command appropriate for your system.

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

Source code that produces bytecode when compiled
python
number = 5
print(number * 2)
Output
10

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

For interviews, explain the distinction between source code, bytecode, the Python runtime, and CPU machine instructions. Avoid claiming that every Python implementation always uses the same bytecode execution process.

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

Indentation defines a code block
python
score = 85
if score >= 50:
print('Pass')
print('Program finished')
Output
Pass Program finished

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

Do not mix inconsistent indentation within a code block. Use consistent spaces and keep the indentation level aligned with the logical structure of the program.

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

A NameError caused by a misspelled name
python
language = 'Python'
print(languge)
Output
NameError: name 'languge' is not defined

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

When debugging, change one thing at a time and run the program again. Do not suppress an error without understanding why it occurred.

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.

Incorrect Approach
script.py
python
Thinking Python is only used by beginners or only for web development.
Correct Solution
script.py
python
Python is a general-purpose language used in automation, web services, analytics, scientific computing, testing, and AI. Its suitability depends on the task.

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.

Incorrect Approach
script.py
python
Assuming that a high-level language does not use machine instructions or a runtime.
Correct Solution
script.py
python
Python provides high-level abstractions, but an implementation still needs a way to execute its program on the underlying system.

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.

Incorrect Approach
script.py
python
Claiming that Python source code always executes directly without compilation.
Correct Solution
script.py
python
In CPython, source code is typically compiled to bytecode, which the runtime evaluates. Other implementations may use different execution strategies.

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.

Incorrect Approach
script.py
python
Believing that the Python virtual machine is the same as the computer’s physical CPU.
Correct Solution
script.py
python
The Python virtual machine is part of the Python runtime and evaluates Python bytecode. The physical CPU executes machine instructions.

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.

Incorrect Approach
script.py
python
Assuming variable names have fixed declared types in Python.
Correct Solution
script.py
python
Python is dynamically typed. Names can be rebound to objects of different types, while the objects themselves retain their types.

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.

Incorrect Approach
script.py
python
Using inconsistent indentation or mismatched quotation marks.
Correct Solution
script.py
python
Use consistent indentation, conventionally four spaces per level, and make sure string delimiters match.

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.

Incorrect Approach
script.py
python
Assuming that a program is correct just because it runs without an exception.
Correct Solution
script.py
python
Compare actual output with expected output, trace the program, and test representative inputs and edge cases.

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.

Practice Arena →

Technical and placement interview questions

Frequently tested interview questions with detailed, full answers (click to expand)

Q1:What is Python?
Answer:
Python is a high-level, general-purpose programming language known for its readable syntax and broad ecosystem. It is used in web development, automation, data analysis, scientific computing, software testing, and artificial intelligence.
Q2:Who created Python, and when was it first released?
Answer:
Python was created by Guido van Rossum. Its first public release was in 1991. The language emphasizes readability and developer productivity.
Q3:Why is Python called a high-level language?
Answer:
Python provides abstractions such as objects, lists, dictionaries, and functions so programmers can focus on the problem instead of manually expressing every low-level machine operation. Its runtime handles many implementation details.
Q4:What does general-purpose mean?
Answer:
It means Python can be used for many categories of software rather than being restricted to one specific domain. Examples include web services, scripts, data analysis, testing, and machine learning.
Q5:How does CPython execute a Python program?
Answer:
In a typical execution, CPython parses the source code, compiles it into Python bytecode, and evaluates that bytecode through its runtime, including the Python virtual machine. The exact implementation details differ across Python implementations.
Q6:What is the difference between source code and bytecode?
Answer:
Source code is the human-readable program written by the developer. Python bytecode is a lower-level representation of instructions used by implementations such as CPython. It is not the same as native machine code for the physical CPU.
Q7:Is Python interpreted or compiled?
Answer:
Python is commonly described as an interpreted language, but that description simplifies the implementation. CPython typically compiles source code into bytecode and evaluates it at runtime. Other implementations may use different strategies, including just-in-time compilation.
Q8:What does dynamically typed mean in Python?
Answer:
Python associates types with objects rather than requiring a variable name to have a declared type. A name can be rebound to an integer and later to a string. The underlying objects retain their own types.
Q9:What is the difference between a syntax error and a runtime exception?
Answer:
A syntax error occurs when Python cannot parse code according to the language grammar. A runtime exception occurs while a program is executing, such as a NameError caused by accessing an undefined name or a TypeError caused by an unsupported operation.
Q10:Why is indentation important in Python?
Answer:
Indentation identifies the body of compound statements such as if statements, loops, and functions. Incorrect indentation can change the program structure or cause an IndentationError. Four spaces per indentation level is the conventional style.

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.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: