This is a cache of https://developer.ibm.com/tutorials/python-beginners-guide/. It is a snapshot of the page as it appeared on 2025-11-21T04:42:17.609+0000.
Python has become one of the most accessible and widely used programming languages in the world. It was created by Guido van Rossum in the late 1980s and released publicly in 1991. It is open source, and today it powers various applications including websites, scientificcomputing, artificial intelligence, automation, and more. This beginner's guide is designed for readers who have never written code before and will demonstrate some Python basics.
Why learn Python?
Python is often recommended as a first programming language because it emphasizes readability and simplicity. Its design encourages you to express concepts in fewer lines of code than languages like c or Java. Python is cross‑platform and can be used for web development, software scripting, mathematics and more recently it has become the de facto language of AI engineers worldwide. Python’s clean syntax and readability make it an excellent first language and understanding the fundamentals provides an excellent foundation of programming.
Major reasons to learn Python include:
Ease of learning: the language reads almost like English and does not require declaring variable types; the interpreter will infer types automatically.
community and libraries: There is a vast ecosystem of packages (such as NumPy, pandas, Django and Flask) that help you build applications quickly.
Versatility: Python is used for automation, data analysis, web services, machine learning and robotics.
Modern improvements: Python 3.13 (released in 2024) offers a friendlier interactive interpreter with coloured prompts, multi‑line editing, recall of previous code blocks and improved paste behavior. Error messages now include coloured tracebacks and suggestions when keyword arguments are misspelled. These changes make learning Python easier than ever.
So what are you waiting for? Let’s get hands-on, and by the end of this guide you will be a programmer!
Setting up Python
Your computer may already include Python, but many systems still bundle Python 2. The official Python beginner’s guide warns that these older versions are outdated and recommends installing the latest Python 3. You can download installers for Windows, macOS and Linux from python.org. Alternatively, use your system’s package manager. You can even run Python on your iPad by using the Pythonista app.
On Ubuntu/Debian
sudo apt update
sudo apt install python3
copy codecopied!
On macOS, download and run the installer from python.org.
On macOS, install python with Homebrew:
brew install python
copy codecopied!
On Windows, download and run the installer from python.org
After installation, verify the version from a terminal or command prompt:
python3 --version
\# The output should show Python 3.11, 12 or 3.13 (or later)
copy codecopied!
Great! You now have Python installed.
choose an editor or IDE
If you downloaded Python using the installer from python.org, you are provided with a simple environment that you can use to run Python code called IDLE. This will suffice for this beginner’s guide but as you progress on your journey you can use more powerful code editors such as VScode, Pycharm, or Jupyter notebooks, all of which provide more comprehensive features for python programming.
You can also just use the command prompt to enter one or multiple commands to test code snippets quickly or generate output for your work. When it comes to more than a few lines of code, it's best to generate and save a program file separately (but more on that later).
The other option, at least on Linux and macOS, is to open a terminal window and type python at the command prompt. Doing so starts the Python command prompt, and you can start typing commands and running Python code.
The interactive interpreter (REPL)
Now that you have Python installed, it provides a read-evaluate-print-loop (REPL) when you run the command python or python3 without a script. If you have Python 3.13 installed, the REPL provides colored prompts, multiline editing and makes experimentation and testing of little scripts even easier.
Assuming you installed using an installer, open the IDLE shell by choosing it from your applications menu, and then try the following:
>>>2 + 2
copy codecopied!
You should get the answer after you press enter: 4.
Let’s try something a little more fun, let’s use a for loop:
Great, you are on your way to becoming a python programmer!
Writing your first Python program
Python includes many built-in functions and you used one of them above called print() to display the output. Now, let’s take the command you ran in the REPL and convert it into a script that you can run as a program. In any text editor, create a file with the following contents and save the file as hello.py.
# hello.py – prints a greetingprint("Hello, world!")
copy codecopied!
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 hello.py.
You should get the response Hello, world! The first line in your file that starts with # is a comment and will be ignored by the interpreter. You should always included comments when you write a program as they help with debugging and will help you keep track of the code you write as you become a more proficient Python developer. congratulations have just written your first python program!
Variables and data types
As mentioned earlier, Python is what’s known as as a dynamically typed language, which basically means that you don’t have to specify a type when declaring a variable and you simply assign variables with values and the Python interpreter infers the type.
Let’s try a couple of examples. Make a new file called types.py and paste the following into it and save it. In the first 4 lines, we are declaring various types. We are then going to use the print function again to print out the type of variable Python thinks it is.
# Assigning variables
age = 30# integer
price = 19.99# floatingpoint number
name = "Alice"# string (text)
is_student = True# boolean (True or False) print(type(age)) # <class 'int'> print(type(price)) # <class 'float'> print(type(name)) # <class 'str'> print(type(is_student))# <class 'bool'>
copy codecopied!
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 types.py.
The output you should see from the terminal window should be like the following:
Python supports many built-in data types but mastering these basics will get you started:
Data type
Sample value
Valid values
int
42
whole numbers, counts, indexes
float
3.1415
decimal numbers, measurements
str
'Hello'
textual data, labels
bool
True
logical values used in conditions
list
[1, 2, 3]
ordered collection of items; can mix types
dict
{'a':1}
mapping of keys to values
tuple
(1, 'x')
immutable ordered collection
Basic Operations
Great, now you know about how to write a python script, execute it, and how to assign values to variables. Now let’s move on to learning about common operations that you will use in real-world scenarios.
Mathematical operations
Python uses familiar arithmetic operators for things like addition, subtraction, multiplication and division as well as a couple of others for performing floor division and remainder calculations.
Make a new python file called operations.py and paste the following lines into it:
a = 7
b = 2print(a + b) # addition (9) print(a - b) # subtraction (5) print(a * b) # multiplication (14) print(a ** b) # exponentiation (49) print(a / b) # true division (3.5) print(a // b) # floor division (3) print(a % b) # remainder (1)
copy codecopied!
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 operations.py.
The output you should see from the terminal window should be as below
9514493.531
copy codecopied!
You have just created 2 variables (a and b), assigned them some values (7 and 2), and then performed many operations on them to get the respective outputs. Step-by-step you are getting python skills and soon will know your way around python projects in no time.
String operations
You are highly unlikely to ever want to write a program in python that only consists of mathematical operations, and working with strings is also a key part of the python programming language. Strings can be enclosed in single ' or double " quotes. Triple quotes (''' or """) allow multi‑line strings. Strings can be concatenated with +, repeated with *, and manipulated using methods like .upper() and .replace(). Python automatically concatenates adjacent string literals.
Let’s make a new file called strings.py and and paste the following lines into it:
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 strings.py
The output you should see from the terminal window should look like this:
Hello world
HELLO Python
This is a
multi‑linestring.
copy codecopied!
You can also use escape characters like \ncreate new lines and \" or \' allow you to include quotes inside strings.
collections
Now that you know how to create variables and perform some basic operations on them, let’s look at another staple of computer science, the concept of a collection. In any program, you typically need to store information in a data structure and one of the most versatile ones that Python provides is the list. Lists are ordered and mutable collections that can hold multiple types of items in them.
You create lists with square brackets [], and you can add items, remove them, slice sub‑lists and iterate over them. Here are some key operations you can perform on a list. Imagine you have a list called lst the following describes what the following commands in python would perform.
Indexing: lst[0] returns the first element; negative indexes count from the end.
Slicing: lst[1:3] returns a new list containing elements at positions 1 and 2.
Appending: lst.append(3) adds an element to the end.
List comprehensions provide a concise way to build lists by looping and optionally filtering; they are both readable and efficient. You should try to use them instead of lengthy loops when appropriate.
control Structures
Now that you know how to declare variables, perform operations on them, and create collections from them, let’s move on to how you can perform decision making in python syntax.
conditional statements
Python uses the following keywords for control the flow of a program: if, elif, and else. If statements (if), else if statements (elif), and else statements (else) form the fundamental building blocks of any program when you use python.
Let’s make a new file called conditionals.py and and paste the following lines into it. Ensure that you have the indentation accurate when you paste otherwise the script will fail to execute.
x = 3if x < 0:
x = 0print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
copy codecopied!
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 conditionals.py.
The output you should see from the terminal window should be like this:
More
copy codecopied!
Edit the conditionals.py file and change the value of x to be 1 or 0, and even -1, and see what happens when you execute the script again.
Loops
We already used a for loop earlier for a little fun but they are very powerful for iterating over a sequence such as a list of string. You will generally use a for loop or while loop and normally will use the built-in range function to generate a sequence of numbers and this combination forms part of the daily routines of a python programmer.
Let’s make a new file called loops.py and and paste the following lines into it:
# For Loop
words = ['cat', 'dog', 'elephant']
for w in words:
print(w, len(w))
# Loop with range for i inrange(5):
print(i)
# While Loop print("We are about to take off in this while loop")
n = 5while n > 0:
print(n)
n = n - 1print('Blast off!')
copy codecopied!
Now open up your terminal and navigate to where you saved the file, and then type the following into the command line: python3 loops.py.
The output you should see from the terminal window should be like this:
cat 3
dog 3
elephant 801234
We are about totakeoffin this whileloop54321
Blast off!
copy codecopied!
Python also supports statements to control the loop such as break (exit the current loop), continue (skip to the next iteration) and an elseclause that executes only if the loop completes without a break.
Functions
Functions are an important concept for anyone learning to program and will be a key way that you will use python modules in your hands-on experience with Python. Fundamentally a function is a process of breaking down a large programming task into smaller subtasks that allow you to declare logic in one place and then execute it in many. Python has many built-in functions and as you write your own functions you will likely import modules from a python library or your own code into your programs and execute them to keep your code clean and minimize duplication of logic.
I grew up in a place where celsius was the standard measure of temperature but now live in a place where fahrenheit is the standard measure. Let’s write a python function to help with the conversion of celsius to fahrenheit.
Let’s make a new file called functions.py and paste the following lines into it:
defcelsius_to_fahrenheit(celsius):
"""convert celsius to Fahrenheit"""return (celsius * 9/5) + 32deffahrenheit_to_celsius(fahrenheit):
"""convert Fahrenheit to celsius"""return (fahrenheit - 32) * 5/9# Lets test the function with different values
test_celsius = 21
fahrenheit = celsius_to_fahrenheit(test_celsius)
print(f"converting {test_celsius}°c to Fahrenheit")
print(f"{test_celsius}°c = {fahrenheit:.1f}°F")
test_fahrenheit = 70celsius = fahrenheit_to_celsius(test_fahrenheit)
print(f"converting {test_fahrenheit}°F to celsius")
print(f"{test_fahrenheit}°F = {celsius:.1f}°c")
copy codecopied!Show more
Now open up your terminal and navigate to where you saved the file, and type the following into the command line: python3 functions.py.
The output you should see from the terminal window should be like this:
converting21°c to Fahrenheit
21°c = 69.8°F
converting70°F to celsius
70°F = 21.1°c
copy codecopied!
In our functions above we are return values using the return keyword but if we don’t have a return function then Python will just return none if we aren’t expecting to provide anything back, normally this would occur if you were generating a log entry or writing something to a file.
Summary
Look at you, you made it. You now know the basics of Python and can easily start to write your own scripts to do many things just using the knowledge you gained above. You learned about variables, data types, how to perform operations on them, organize your data into collections, and turn them into distinct functions.
Next steps
The web offers hundreds of sites to help you learn Python and there are many python courses available depending on whether you are learning it for web development, data science, or machine learning that provide you with the relevant python skills.
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review yourcookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.