Last updated on
Python Fundamentals for Beginners
A beginner-friendly look at the basics of Python, covering variables, data types, collections, and operations, with code examples.
This guide covers a few Python fundamentals: variables, common data types, collections, and basic operations. Each section includes a small example you can run and change.
Getting started with Python
Before working through the examples, make sure Python is installed on your computer. In this video, I show how I install Python while using the NVDA screen reader.
Watch the video
Loading this video connects you to YouTube (Google), which receives your IP address and may store or read information on your device for playback and advertising. YouTube may show ads. Choose Load YouTube video to allow this for this video. See Google's privacy policy.
Run the examples
The quickest way to try an example is the Python prompt. Open a terminal and type python (or py) on Windows, or python3 in Ubuntu on WSL, then press Enter. At the >>> prompt, type a line and press Enter to run it. Typing a variable name on its own line reads back its value. Type exit() to leave the prompt.
For longer examples, save the code in a file such as practice.py and run python practice.py from the folder the file is in. In a file, use print() to hear a value in the output.
Python uses indentation to group code, so if you have not already, turn on spoken indentation in NVDA Settings for Coding.
Variables
A variable gives a name to a value so you can use that value later. For example:
age = 25
name = "John"
Here, age refers to the integer 25, and name refers to the string "John". Python determines the type from the value you assign.
Data types
Python includes several built-in data types. These examples cover a few you will use often.
Numbers
Two common numeric types are integers (int) and floating-point numbers (float).
num_int = 10
num_float = 3.14
The num_int variable holds an integer, while num_float holds a floating-point number. These data types are used for mathematical operations and calculations.
Strings
Strings are sequences of characters used to store text.
message = "Hello, World!"
The message variable contains the string "Hello, World!". Strings can be manipulated in various ways, such as concatenation, slicing, and formatting.
Collections
Python provides several built-in data structures to store collections of items. The most commonly used are lists and dictionaries.
Lists
Lists are ordered collections that can store multiple items.
my_list = [1, 2, 3, 4, 5]
You can access elements in a list using indexing, and they are mutable, meaning you can modify them:
my_list[0] = 10 # Changing the first element to 10
Dictionaries
Dictionaries use key-value pairs to store data.
person = {'name': 'John', 'age': 25, 'city': 'New York'}
Access a value by using its key:
print(person['name']) # Output: John
Basic operations
Python includes operators for working with data. Here are two basic examples.
Arithmetic operations
Python supports standard arithmetic operations like addition, subtraction, multiplication, and division.
result = 10 + 5
The result variable holds the sum of 10 and 5.
String operations
Strings support various operations, including concatenation and slicing.
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
Here, full_name combines the values of first_name and last_name with a space in between. You can also slice strings to extract parts of them:
greeting = "Hello, World!"
print(greeting[0:5]) # Output: Hello
What to practice next
Run each example, change the values, and listen to the output. Then combine the concepts by creating a dictionary for a person, putting several dictionaries in a list, and printing one value from each item. That small exercise uses variables, strings, numbers, lists, dictionaries, and indexing together.