Introduction to Python
By the Syncfusion DevTutorials team · Last updated
What Python is, why people learn it, where it is used, how to run your first program, and the three tools every program needs: print(), input() and comments.
Imagine you are giving instructions to a friend who has never cooked before.
If you say "make tea", your friend will be confused. There are too many missing steps.
So you write the steps down in order:
- Boil water.
- Add tea leaves.
- Add milk and sugar.
- Pour it into a cup.
Now your friend can follow along without guessing.
A computer needs the same kind of help. It is fast, but it does not know what you want. You have to write the steps down, in order, in a language the computer understands.
Python is one of those languages. You write the steps, and Python carries them out.
What You Will Learn
In this lesson, you will learn:
- What Python is, and what the words "interpreted", "high-level", "dynamically typed" and "object-oriented" mean
- Why so many people choose Python as their first language
- Where Python is used in real jobs
- How to run Python, both here on this page and on your own computer
- How to show information with
print() - How to ask the user a question with
input() - How to write comments, which are notes for people reading your code
Before we go further, here is a complete Python program. It is one line long.
Your first Python program
print("Hello, World!")
Press Run. The words appear below the code.
That is the whole idea. You write an instruction, Python follows it, and you see the result.
Python is a language you use to tell a computer what to do, step by step.
What Is Python?
Python is a programming language. Just as people use spoken languages to talk to each other, programmers use programming languages to talk to computers.
A programming language is a set of words and rules you use to give instructions to a computer. Some words tell the computer to show something on the screen. Others tell it to remember a value or to make a choice. When you put these words together in the right order, you get a program.
Python was created by Guido van Rossum and released in 1991. He wanted a language that was easy to read. That choice is the reason Python looks the way it does today — it was designed, from the start, to read almost like everyday English.
Look at this program:
Python reads like English
marks = 78
if marks >= 35:
print("Asha passed the exam")
You can almost read that out loud as a sentence. Marks are 78. If marks are 35 or more, print that Asha passed.
Many languages describe Python with four words, and you will meet them again and again as you learn: interpreted, high-level, dynamically typed, and object-oriented. Each one sounds technical. Each one is simple once you see what it means.
Interpreted
Interpreted means Python runs your code line by line, straight away.
Some languages work differently. They need a separate step first, called compiling, which turns your whole program into a file the computer can run. Only then can you see the result. That extra step exists for good reasons in large professional projects, but it adds waiting while you are learning.
Python skips that step. You write a line, you run it, you see what happens.
Because there is no waiting between writing and seeing, you can try an idea as soon as you have it. You type one small change, press Run, and the answer is there immediately.
Each line runs in order, top to bottom
print("Step 1: boil water")
print("Step 2: add tea leaves")
print("Step 3: pour into a cup")
The three lines ran in the order you wrote them. Python started at the top and worked down — the same order as the steps in the tea instructions at the top of this page.
High-Level
High-level means Python hides the complicated parts of the computer from you.
A computer stores everything as numbers in memory. It cares about memory addresses and byte sizes. In a low-level language, you have to manage those details yourself, which is slow and easy to get wrong.
Python takes care of them. You write name = "Asha" and Python works out where to keep it. You never see the memory address, and you never need to.
You get to think about the problem you are solving, not about the machine. In practice, that means a beginner can write working programs long before understanding how a computer stores data — the details arrive later, if ever.
Dynamically Typed
A type is the kind of a value. "Asha" is text. 78 is a whole number. 92.5 is a decimal number. Knowing the type matters because what you can do with a value depends on what kind it is — you can do arithmetic with numbers, but not with names.
Dynamically typed means you do not have to announce the type. Python looks at the value and works the type out for itself.
Python works out the type on its own
name = "Asha"
marks = 78
average = 92.5
print(type(name))
print(type(marks))
print(type(average))
You never said "this one is text" or "this one is a whole number". Python decided, based on what you stored. The type() instruction simply tells you what Python decided, which is handy when you are unsure.
str is short for string, which means text. int is short for integer, which means a whole number. float means a number with a decimal point. You will see these three names many times in this course, so it is worth learning to recognise them now.
A type is the kind of a value. Text, whole number, decimal number. Python works out the type for you.
Object-Oriented
In Python, every value is an object. An object is a value that carries its own set of actions.
This is easier to see than to explain. Text knows how to make itself uppercase. A list knows how to count its own items. You do not have to write that code — it comes with the value, ready to use.
Values carry their own actions
name = "asha"
print(name.upper())
print(name.capitalize())
print(len(name))
The dot means "use an action that belongs to this value". name.upper() asks the text to give back an uppercase copy of itself, and name.capitalize() gives back a copy with a capital first letter. You write the value, a dot, and the action you want.
Notice that name itself did not change. upper() handed back a new piece of text and left the original alone. Actions often work this way, and you will use this pattern constantly in later lessons.
Why Learn Python?
There are hundreds of programming languages, so why start with this one? Python has four qualities that make it a favourite first language, and each one is worth understanding in turn.
Easy to Learn
Python uses fewer symbols than most languages. There are no curly brackets around blocks and no semicolons at the end of lines. Instead, Python relies on plain words and simple indentation, which keeps your code clean and readable.
Compare the same idea in two languages.
In Java, printing one line looks like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In Python, it is this:
The same job, one line
print("Hello, World!")
Both programs print the same words. The Java version needs a class, a method, and several layers of brackets before anything happens — and every one of those must be typed correctly. The Python version has one line, less to remember, and therefore less to get wrong while you are still learning.
Large Standard Library
A library is a collection of code someone else already wrote, ready for you to use. Instead of building everything yourself, you borrow from a library the way you might use a dictionary instead of writing your own.
Python comes with a large set of these, called the standard library. It is already installed. You do not download anything.
Code that came with Python
import math
import random
print(math.sqrt(144))
random.seed(7)
print(random.randint(1, 6))
import brings in a library. math does mathematics — here it finds the square root of 144. random picks random values — here it rolls a number from 1 to 6, like a die.
random.seed(7) tells Python which random sequence to use. With the same seed, you get the same number every time, so this page can show you a fixed result. Leave the seed out in your own programs and the number really does change each run.
The standard library has tools for dates, files, web addresses, zip files and much more. Whatever small job you need, it is often already sitting there, waiting for an import.
Cross-Platform Support
Cross-platform means the same program runs on Windows, macOS and Linux.
You write the file once. You can copy it to a different computer and run it there without changing it. The program you write on your laptop at home will run on a school machine or a work server just the same — no separate version is needed for each system.
Open Source
Open source means Python is free, and anyone can read the code Python itself is built from.
You do not pay for it. You do not need a licence. Thousands of people improve it together, which is why Python keeps gaining new features and fixes year after year. For you as a learner, it simply means there is nothing to buy and nothing to unlock — you can download it and start today.
Easy to read, batteries included, runs everywhere, costs nothing. Those four points are why Python is the most common first language taught today.
Applications of Python
Python is not only for learning. It is used in real work every day. The tour below shows the main areas where Python programmers are employed.
Web Development
Websites need code running on a server to handle logins, save data and build pages. The page you see in your browser is only half the story — behind it, server code checks who you are, stores your choices and prepares what to show you next. Python does that job in frameworks such as Django and Flask.
Automation and Scripting
A script is a small program that does one repetitive job for you.
Renaming two hundred files. Reading a folder of spreadsheets. Sending a reminder email every Monday. These are boring by hand and quick in Python. Many workplaces value exactly this kind of small automation, and it is one of the fastest practical uses of Python to learn.
Data Science
Data science means finding useful facts inside large amounts of data.
Every large organisation collects data — sales numbers, exam marks, website visits. The interesting part is what that data tells you, and that is what data science is about.
A small piece of data work
marks = [78, 92, 65, 88, 71]
print("Highest:", max(marks))
print("Lowest:", min(marks))
print("Average:", sum(marks) / len(marks))
Five marks fit on one line here. The same three instructions work just as well on five million — the code does not get longer, only the data does. Python is the language most of this data work is done in.
Machine Learning
Machine learning means teaching a computer to spot patterns from examples instead of writing out every rule.
Spam filters and film recommendations work this way. Nobody writes a rule for every scam email; instead, the program learns from thousands of examples. Most of the well-known tools for it are built for Python, which is why almost every machine learning course starts here.
Desktop Application Development
Python can also build programs with windows and buttons that run on your own computer, using toolkits such as Tkinter and PyQt. Simple tools like a to-do list app or a calculator can be built in an afternoon once you know the basics.
| Area | What Python does there | Well-known tools |
|---|---|---|
| Web development | Runs the server behind a website | Django, Flask |
| Automation | Does repetitive jobs for you | Standard library |
| Data science | Finds facts inside large data | pandas, NumPy |
| Machine learning | Learns patterns from examples | scikit-learn, PyTorch |
| Desktop apps | Builds windows and buttons | Tkinter, PyQt |
Running Your First Python Program
There are two ways to run Python. Both are useful, and they are useful at different times — interactive mode for quick questions, script mode for real programs. On this page, you get a third option: the editor built into the lesson.
Interactive Mode
Interactive mode means you type one line and Python answers immediately.
You start it by typing python in a terminal. You then see three arrow symbols. That is the prompt, and it means Python is waiting for you.
>>> 2 + 3
5
>>> name = "Asha"
>>> name.upper()
'ASHA'
>>> exit()
Python answered after every line. Nothing was saved. When you close it, your work is gone.
Interactive mode is for trying one small thing. "What does this do again?" is the question it answers best. There is no file to create and nothing to set up — you type a question and get the answer immediately.
Script Mode
Script mode means you write your instructions in a file and run the whole file at once.
You save the file with a name ending in .py, such as marks.py. Then you run it:
python marks.py
Python reads the file from the top, runs every line, and stops at the end.
Script mode is for real programs. The file is saved, so you can run it again tomorrow, fix it, and share it. Anything you want to keep — anything longer than a quick experiment — belongs in a file.
| Interactive mode | Script mode | |
|---|---|---|
| How you start it | Type python | Type python filename.py |
| How much runs | One line at a time | The whole file |
| Is it saved? | No | Yes |
| Best for | Trying something quickly | Real programs |
The Editor on This Page
You do not need to install anything to follow this course.
Every code box on this page with a Run button is a real Python editor. Python runs inside your browser, on your own computer. Nothing is sent anywhere.
You can change the code in any of these boxes and run it again. Breaking an example on purpose is one of the fastest ways to learn what it does — change something, run it, and see what changes in the result. Restore Code puts the original back.
The editor checks your code as you type. If you leave a bracket or a quote mark open, or forget a colon, the line is marked in red before you press Run. Mistakes that only appear while the program is running, such as using a name you never stored, still wait for Run.
Change a number, change a word, press Run again. You cannot damage anything, and the result tells you more than reading does.
The Hello World Program and Its Output
The first program people write in any language prints the words "Hello, World!". It is a tradition, and a good one: it is the shortest program that actually does something visible.
Hello World, taken apart
print("Hello, World!")
That one line has four parts worth naming.
printis the name of the instruction. It means "show this".- The round brackets
(and)hold what you are giving toprint. - The quote marks
"and"mark the start and end of some text. Hello, World!is the text itself.
The output is the text without the quote marks. The quotes tell Python where the text starts and stops. They are not part of it — think of them as the wrapping paper, which is thrown away once Python knows where the text ends.
Leave the quote marks out and the program stops with an error:
Text without quote marks
print(Hello)
Python read Hello as the name of something you stored earlier. You never stored anything by that name, so it stopped and said NameError. An error is not a disaster. It is Python telling you exactly which line it could not follow. Read the message, fix the line, and try again — that rhythm is a normal part of programming, not a sign of failure.
Input and Output in Python
What Input and Output Mean
Output is information going out of the program to the person using it. Text appearing on the screen is output.
Input is information coming into the program from the person using it. Someone typing their name into your program is input.
A program that only has output says the same thing every time it runs. A program with input can respond to whoever is using it — each person who runs it gets an answer based on what they typed.
User Interaction in Python Programs
Most useful programs follow the same three steps:
- Ask the user for something. That is input.
- Do something with it.
- Show the result. That is output.
Here is that shape in a real program:
Input, work, output
name = input("What is your name? ")
print("Welcome,", name)
The Input box under the code holds what the user types. This one holds Asha. Change it to your own name and press Run again.
Python's two main tools for this are input() for getting information in, and print() for sending information out. Almost every program you write in this course will use at least one of them, so it is worth getting comfortable with both.
input()
What input() Does
input() stops the program, waits for the user to type a line, and hands that line back to you.
Everything waits. Nothing else happens until the user presses Enter. This may not seem important now, but it explains a lot of program behaviour later: the program genuinely pauses at that line until it receives an answer.
Syntax of input()
variable = input("message to show the user")
- The message in the brackets is shown to the user. It is called the prompt.
- Whatever the user types is stored in the variable on the left.
- The prompt is optional.
input()on its own waits without saying anything.
A good prompt tells the user exactly what to type, like Enter your name: . Without it, the user sees a program that has simply stopped, with no idea what is expected.
Reading User Input
Reading one line from the user
name = input("Enter your name: ")
print("Hello,", name)
The prompt appeared, Python took the line from the Input box, and stored it in name. The print() line then used that stored value to greet the person who typed it.
Taking Multiple Inputs
Call input() once for each thing you want to ask for.
Two questions, two answers
name = input("Enter your name: ")
section = input("Enter your class: ")
print("Name:", name)
print("Class:", section)
Look at the Input box. It has two lines. The first input() took the first line, and the second took the second line. One line per question, in order. If a program asks three questions, the Input box needs three lines — lined up in the same order as the questions.
Type Conversion with input()
This is the single most common surprise in beginner Python, so it is worth meeting it now.
input() always gives you text. Even when the user types a number.
Watch what goes wrong:
Adding two numbers the wrong way
first = input("Enter the first number: ")
second = input("Enter the second number: ")
print("Total:", first + second)
The total of 20 and 5 should be 25. Python printed 205.
Nothing is broken. Python did exactly what you asked. It had two pieces of text, "20" and "5", and joining two pieces of text with + sticks them together end to end.
To do arithmetic, you have to turn the text into a number first. That is called type conversion.
int()turns text into a whole number.float()turns text into a decimal number.
Adding two numbers correctly
first = int(input("Enter the first number: "))
second = int(input("Enter the second number: "))
print("Total:", first + second)
Now the answer is 25.
Read the inner brackets first. input() collects the text, and int() turns that text into a number before it is stored. Working from the inside out is how you read nested brackets in general — you meet the same pattern everywhere in Python.
input() always hands back text. Wrap it in int() or float() before you do any arithmetic with it.
Common Input Examples
Three kinds of answer
name = input("Name: ")
age = int(input("Age: "))
average = float(input("Average marks: "))
print("Name:", name)
print("Next birthday:", age + 1)
print("Rounded average:", round(average))
Three answers, three different treatments. The name stays as text. The age becomes a whole number, so you can add to it. The average becomes a decimal number, so you can round it. The lesson to take away is that you choose the conversion to match what you plan to do with the answer.
print()
What print() Does
print() shows information to the person running the program.
It is how a program tells you what it is doing, what it found, and what went wrong. Without output, a program might work perfectly and you would never know. Real programs use print() constantly to keep you informed.
Syntax of print()
print(value1, value2, ..., sep=" ", end="\n")
- You can give
print()as many values as you like, separated by commas. sepis what goes between the values. A single space, unless you change it.endis what goes after the last value. A newline, unless you change it.
Most of the time you only need the values. But sep and end come up in the next two sections, so it is worth knowing they exist.
Printing Text and Variables
Text and stored values
student = "Asha"
marks = 78
print("Report card")
print(student)
print(marks)
Text in quote marks is printed as it is written. A name without quote marks is treated as something you stored, and its value is printed instead. This distinction matters — "Asha" prints the word Asha, while student prints whatever is stored in the variable student.
Printing Multiple Values
Put commas between values and print() shows them all on one line.
Several values in one line
student = "Asha"
marks = 78
print("Student:", student, "Marks:", marks)
Look closely at the spaces. You did not type a space after Student:, but there is one in the output. print() puts a single space between values for you. It is a small kindness, but one you should know about, or the extra spaces in your output will confuse you.
Using the sep Parameter
sep changes what goes between the values. Instead of a space, you can put a dash, an email symbol, or nothing at all.
Choosing the separator
print("2026", "03", "14", sep="-")
print("asha", "school", "in", sep="@")
print("A", "B", "C", sep="")
The first line builds a date. The second builds an email address. The third joins the letters with nothing at all between them. One setting does three very different jobs, just by changing what sits between the values.
Using the end Parameter
Normally print() moves to a new line when it finishes. end changes what it does instead.
Staying on the same line
print("Loading", end="")
print(".", end="")
print(".", end="")
print(". done")
Four separate print() calls, one line of output. Each of the first three ended with nothing, so the next one carried on where it left off.
Without end="", the same four lines would print four separate lines. This is handy for progress messages, building a line piece by piece, or anything where one line is meant to grow gradually.
Formatted Output Basics
Joining text and values with commas works, but it gets hard to read. The more values you add, the harder it becomes to see where each one lands in the sentence.
An f-string is a neater way. Put the letter f before the opening quote, then write your values inside curly brackets, right where you want them.
An f-string puts values inside the text
student = "Asha"
marks = 78
print(f"{student} scored {marks} marks")
print(f"{student} needs {100 - marks} more for full marks")
The text reads like a sentence, and you can see exactly where each value lands. You can even do a small calculation inside the brackets, as the second line does. Because the values sit directly in the sentence, there are no surprise spaces and no counting of commas.
Use commas for a quick line. Use an f-string when the sentence matters, which in real programs is most of the time.
Comments
What a Comment Is
A comment is a note inside your code for people to read. Python ignores it completely.
This means a comment has no effect on what the program does. You can write anything in a comment — a reminder, a question, an explanation — and the program runs exactly the same.
Why Comments Are Used
Code says what the program does. A comment says why it does it.
Six months from now, you will not remember why you wrote a line. Neither will anyone else reading your work. A comment answers that question, saving the next reader — often your future self — the work of puzzling it out.
Single-Line Comments
A comment starts with #. Everything after it on that line is ignored.
A comment is skipped
# The pass mark set by the school for 2026
pass_mark = 35
print("Pass mark:", pass_mark)
The first line never ran. Python saw the # and moved on. Notice how useful the comment is here — pass mark 35 is a fact from outside the code, and the comment records where it came from.
Inline Comments
A comment can also sit at the end of a line of code. The code runs first, and the note follows after the #.
A note at the end of the line
marks = 78 # out of 100
weight = 0.5 # this test is worth half of the final grade
print("Weighted score:", marks * weight)
Everything before the # runs as normal. Everything after it is a note. Inline comments are best kept short — a few words at most, since they share the line with working code.
Multi-Line Comments
Python has no special symbol for a comment spanning several lines. You put # at the start of each line.
A longer note
# Grade rules agreed with the class teacher:
# 80 and above -> A
# 60 to 79 -> B
# 35 to 59 -> C
# below 35 -> fail
marks = 78
print("Marks:", marks)
This is the recommended way to write long notes in Python, and it is what you will see in most well-written code.
You will also see triple-quoted text used as a block comment:
Triple quotes are text, not a comment
"""
This looks like a comment, and people use it as one.
Python actually treats it as a piece of text that nobody keeps.
"""
print("The program carries on")
That works, but it is not really a comment. Python builds the text and then throws it away. When this block is the first thing in a file or a function, it has a real name and a real job: it is a docstring, the official description of that code. You do not need docstrings yet — just recognise them when you see them, and prefer # for your own notes.
Best Practices for Writing Comments
| Do | Don't |
|---|---|
| Explain why a line exists | Repeat what the line already says |
| Note a rule that came from outside the code | Leave a comment behind after changing the code |
| Mark a part you know is unfinished | Comment out old code and leave it there forever |
| Keep it short and plain | Write a paragraph nobody will read |
A comment that repeats the code adds nothing:
marks = 78 # set marks to 78
A comment that explains the reason earns its place:
marks = 78 # taken from the term 2 answer sheet, not the practice test
The difference is simple: the first comment says what the line says anyway, while the second adds information the line does not contain. Write comments of the second kind.
Common mistakes
Each row is one mistake. Read the middle column first: that is what you will actually see on screen. Every beginner makes several of these, so if one happens to you, find the row, apply the fix, and carry on.
| Mistake | What you see | Why it happens | Fix |
|---|---|---|---|
| Adding two numbers that are really text | 205 instead of 25 | input() always hands back text, and + joins two pieces of text end to end | Wrap each input() in int() |
| Forgetting the quote marks | NameError: name 'Hello' is not defined | Without quote marks, Python reads Hello as the name of something you stored earlier | print("Hello") |
| Missing one of the round brackets | SyntaxError: '(' was never closed | Every opening bracket needs a closing one | Add the ). The editor marks the line as you type |
| Capital P in Print | NameError: name 'Print' is not defined | Python treats capital and small letters as different | Write print, all in small letters |
| Expecting a comment to run | The line does nothing, however often you run it | A # in front of a line makes Python skip the whole line | Remove the # if you meant the line to run |
| Asking for more input than you gave | EOFError | Every input() call needs its own line in the Input box | Add another line to the Input box |
| Converting something that is not a number | ValueError: invalid literal for int() | int() was handed text such as twelve or an empty line | Check what the Input box holds |
| Mixing tabs and spaces to indent | IndentationError or TabError | Python decides what belongs to a block by counting the spaces in front of it | Use four spaces for every level, never a tab |
What to Remember
- Python is a language you use to tell a computer what to do, step by step.
- Interpreted means Python runs your code line by line, with no separate build step.
- High-level means Python handles the machine's details so you can think about the problem.
- Dynamically typed means Python works out the type of a value for you.
- Object-oriented means every value carries its own actions, reached with a dot.
- Interactive mode is for trying one line. Script mode runs a saved
.pyfile. print()shows information.sepchanges what goes between values,endchanges what goes after.- An f-string, written
f"{name}", puts values inside your text where you want them. input()always gives you text. Wrap it inint()orfloat()before doing arithmetic.- A comment starts with
#. Use it to say why, not what.