A Primer on Python Data Types and Syntax

Your first baby steps learning Python

Jonathan Hsu
Apr 15, 2020 · 4 min read

Learning data types and basic syntax is the equivalent of learning about nouns, verbs, and punctuation — it forms the foundation that all communication will be based on.

This is the first step in learning any new language, spoken or programming. Use this primer as an introduction to Python basics. After reading this, you’ll be ready to get your feet wet and start learning about operators and how to interact with each data type.

Basic Data Types

The following data types — called primitive data types — are widely used in most programming languages. Each data type holds a single value.

  • String: Character values that are enclosed in either single or double quotes.
  • Integer: Whole number values including both negative and positive numbers as well as zero.
  • Float: Decimal number values that are stored as a floating-point expression.
  • Boolean: A true or false value expressed in Python as True or False.
  • None: A value that represents the absence of a value — the equivalent of null in other programming languages.

Intermediate Data Types

The following data types — called complex data types — are a step up from their primitive siblings. These data types are unique to Python, although other languages have analogous data types and all hold multiple values.

We’ll define each data type as above as well as show an example definition.

Tuple

An ordered, immutable container. Tuples cannot be modified once defined positioning them as a constant complex data structure. Each value is referenced by an index, which is an integer beginning at zero.

Tuples are defined by using parenthesis and separating each value with a comma.

("Protoss", "Terran", "Zerg")

List

A list is an ordered, mutable container. Similar to tuples, each value in a list is referenced by its index. Unlike tuples, a list can be modified by adding, editing, and deleting values.

Lists are defined by using square brackets and separating each value with a comma.

[99, 94, 87, 100]

Set

A set is an unordered, mutable container. Being unordered, values in a set are not referenced by an index value. Additionally, sets only contain unique values meaning that no two values will ever be duplicates within a set.

Sets are defined by using curly braces and separating each value with a comma.

{17, 5, 22, 34, 15}

Dictionary

A dictionary is an unordered, mutable set of key-value pairs. Each value in a dictionary is referenced by an identifier, often called a key. Keys in a dictionary must be unique and are normally string values.

Dictionaries are defined by using curly braces and separating each key-value pair with a comma. Keys and values are separated by a colon.

{ "name": "Code 85", "twitter": "85_code" }

Python Syntax Primer

Now that we’ve introduced the data types that you’ll be using 95% of the time, let’s see what that looks like in actual code as we take a crash course on Python syntax.

Python is considered a high-level programming language. What that means is that it is relatively closer to human readable than machine readable… basically it is easier to read and write. You’ll see this quality as we review three key qualities of Python syntax.

Creating Variables

In Python, create a variable upon first use by assigning a value to it.

first_name = "Jonathan"

There is no need to initialize the variable before use, nor is there a need to declare a data type. Python is called a loosely-typed language meaning that variables do not require a data type upon creation.

There are many tips and tricks for creating variables such as defining multiple variables in a single line.

first, second, third = 1, 2, 3
print(first,second,third) # 1 2 3

One word of caution, do not attempt to declare multiple levels into a complex data structure from the intermediate data types.

obj = {}
obj.school.name = "GMU"
# AttributeError: 'dict' object has no attribute 'school'

Looking at the example above, school has not been defined so you cannot assign the value GMU as a nested value.

Indentation

There is no syntax more important in Python than indentation. Other languages will use semi-colons, parentheses, curly braces, and other symbols to define lines and code blocks.

// JavaScript Example
let year = 2020;
if(year < 2020) {
// This is the if statement
}
// Remainder of the code

Python has minimal structural syntax, being able to rewrite the same JavaScript example without many of the symbols.

year = 2020if year < 2020:
# This is the if statement
# Remainder of the code

Being new to Python, it is critical to be conscientious of your indentation because this will cause errors.

age = 35
if age > 18:
print("You can vote") # IndentationError: expected an indented block

While indentation may be frustrating at first, rest assured that it will become second nature and you’ll have significantly easier to read code as a result. In fact, if you follow our Python Getting Started Guide, then you should be using Visual Studio Code or another IDE. These software often will auto-correct your indentation either as you type or when you save, alleviating much of the frustration.

We’ve presented a lot of information to you, but this will carry you quite far in your beginning days of Python. Here are the three biggest takeaways to help reinforce this article’s content:

  • Basic data types hold a single value while intermediate data types are containers that hold multiple values.
  • Define variables when you need to use them
  • Indentation is a syntactic requirement, not a stylistic option

Share your questions, experiences, and comments below. Thanks for reading!

Code 85

Plain language programming tutorials for non-developers

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store