Skip to Main Content
University of York Library
Library Subject Guides

Coding: a Practical Guide

Python

Python

Python is a useful, general purpose coding language that can be used for a range of things, including working with data, machine learning, and creating software.

What is Python?

Python is a coding language used for a wide range of things, including working with data, building systems and software, and even creating games. It's a good place for beginners to start because there are a lot of resources out there for learning Python and there's lots of different directions you can take it.

Should I learn Python?

It might be that your course or job requires you to know some Python, as it's commonly used for research and in a range of contexts. If so, the resources on this page will give you ways of getting started and learning more.

If you're looking for a way to work with your research data - which may be in a variety of formats - or learn a coding language that can offer that possibility, Python is a good option. Python is also used for machine learning, if that's a direction you might want to go in.

If you're looking to upgrade your coding skills for future employment or just for interest, Python can be handy, as it has a lot of transferable skills and is a well-recognised language.

Though Python is used for certain elements of web development, if you're looking to start learning how to make websites, it probably isn't the best starting point.

Key concepts in Python

The Introduction to coding resources cover some key coding concepts and indeed provide some basic Python examples, but here's some more detail on some key things that are useful to know in Python.

You might also want to work through the 'Getting started with coding in Python' workbook to try out some of the concepts in action, with supporting explanations.

Variables and data types

Variables are how you can store values in the computer's memory to work with in your Python code. A variable is given a name and a value. You can then use the variable by referencing its name, and change the value if needed (hence it is 'variable', or can vary).

Variables have different data types, depending on what kind of information they store. This allows the computer to know what kind of actions can be done with that information, for example whether or not you can do arithmetic with it.

To create a variable in Python, you just need to give it a name by typing one, then use an equals sign (=) to assign it a value. Variable names in Python can only start with either a letter or an underscore and they cannot include any spaces. For more information on what you can include when naming a variable, see w3school's guidance on Python variable names.

There are many different data types in Python, but here are some useful ones to start with:

  • Strings - Some characters, such as letters, symbols, or numbers, which are treated as 'text'. They must be surrounded by either single or double quote marks.
  • Integers - Positive or negative whole numbers.
  • Floats - Floating point numbers, shortened to float, are decimal numbers, either positive or negative.
  • Booleans - The Boolean data type is data that is either True or False (it can be shortened to bool).
  • Lists - Lists allow you to put multiple values together in one variable, separated by commas, so you can access them as a group or individually.

Lists in Python

As mentioned above, lists in Python are a way of storing multiple separate values in one variable. This means you can work with the whole collection as well as individual items by accessing them using an index number or position in the list.

Lists are stored in a particular order, which can have duplicate values, and to which you can add, remove, and reorder values. Lists must be written in square brackets, with each value separated by a comma. Items in a list can be different data types, like strings, numbers, or even another list!

Python list indexes start from 0, so to access the first item in the list with the name my_list you would use my_list[0], to access the second item you'd use my_list[1], etc.

For more on lists, see w3schools' pages on Python lists.

Operators: doing things with variables

Operators are the Python commands you use to do things with values, whether or not they are stored in a variable. They are mostly represented by punctuation characters, though some of their meanings might not be what you'd expect.

  • Assignment operators are used to assign values to variables. The equals sign = is most common, as it is used to assign a value to a variable, either when creating it or when changing the value. You might also use =+ to increment a variable's value by 1 and assign it that new value.
  • Arithmetic operators are used to do mathematical calculations, like +, -, *, and /.
  • Comparison operators are used to compare values, e.g. when making decisions. So you might use == to compare if two values are equal, != to compare if values aren't equal, or the greater or less than symbols for checking which value is greater.
  • Logical operators are used to combine statements when making decisions in code, using keywords like and, or, and not.

Comments in Python

Comments are crucial in any coding langauge as a way of explaining to yourself and others what your code is doing, adding important notes, and translating your pseudocode notes into actual code.

In Python, you tell the computer to ignore a line (or the rest of a line) by using a hash # symbol. Once the computer reaches a # it will know the rest of the line is a comment, not code to be executed, and will skip ahead to the next line.

Use comments to note what parts of your code do or to add information that you or someone else might find useful in the future. Comments are also a way of stopping a line or lines of code running whilst you test or try and troubleshoot it. Adding a # to the start of a line of code will mean it will not run.

Loops in Python

Loops are used in Python to repeat code multiple times, either for a particular set of values or while a statement remains true. So this might be for a particular list of values (like some names) or until a changing numerical variable is above or below a certain value.

This means there are two kinds of loops, for loops and while loops, and we're emphasising those words because they are how you create these loops.

The for keyword in Python is how you start a for loop. If you're going to loop through a Python list, you then create a variable that stands for the individual values in that list, then use the keyword in, then finish with the name of the list in question, and a colon. So you'll end up with something like:

for item in shopping_list

where shopping_list is a list variable you've already created.

The while keyword creates a while loop, which means that instead of running through values in turn, the loop returns to evaluate a condition each time the code runs, to check whether it should still be running. This makes while loops useful if you don't know when the loop will need to stop, but want the code to make that decision based on the condition you give. A while loop is created by starting with while, then defining a condition to be tested each time, and ending the line with a colon.

Note: In Python, all the code inside the loop (after the colon) must be indented, as this tells the computer which code is in the loop. Once the indentation ends, that is the end of the loop.

To see more about these loops and have a go, see the Loops section of the Getting started with coding in Python workbook.

IF conditions in Python

As well as automating repetition, Python can be useful for also automating whether or not certain code should run. By giving the computer a statement to evaluate, you can make it decide whether or not the subsequent code should run, using an if condition. The statement can be evaluated to either True or False, and a True statement will make the code run.

There are multiple levels you can have to an if condition. On the most basic level, you can just give one option, to run some code if the statement is True, or skip past the code without an alternative if it is False. Or you can offer an else as well: code that runs if the statement was False. You can also add extra statements that could be true into your condition using elif, short for 'else if'.

Like a loop, you start an if condition with a keyword, in this case the if keyword. You then write your condition to be evaluated, within brackets, and then end the line with a colon. As with loops, any code that should happen if the code is True needs to be indented, to show the computer which code is inside the condition. A simple if condition might look like:

if (day == 'Friday'):
    print('Yay it is Friday')
else:
    print('Sadly it is not Friday')

For more examples of if conditions, see the if conditions section of the Python workbook.

Introduction to programming with Python

Want to learn Python from scratch with a focus on working with data?

Our Introduction to programming with Python course uses the Software Carpentry materials as a basis for getting to grips with the basics of Python. Staff and Research Postgraduates at the University can book onto the next run of the course on the LMS (Staff) or SkillsForge (PGRs) by searching for 'Python'.

Whether you're taking the course or not, below are details on how you can work through the Software Carpentry 'Programming with Python' course, with tips and advice.

You'll need to have a way to run Python to work through the material. There are various ways of doing this, but using a Python notebook is a good method if you're new to coding. The setup guidance document linked below has details for the session, but the instructions can be followed even if yuo're not attending the live session.

If you using a Google account, you might want to try use Google's Colab notebooks, which are a web-based way to write and run Python code. These are also useful for sharing code with other people.

The Software Carpentries materials have setup guidance on how to install Python and Jupyter notebooks on your device.

Tip

If you're using a Google Colab notebook to work through the Software Carpentries materials, you will need to be able to access the data files through your Google Drive. We have a help guide for how to access files using Python notebooks that goes through the steps and the commands you'll need to use.

Python libraries

Python has a range of commands built in, but as Python is used for a huge range of things, there are also many more commands that people have created to make it easier to do particular tasks in Python. These commands can be part of libraries, which are packages of Python code and files that will allow you to use these commands. That code is often called a module. You can create your own modules to put together functions that can be reused.

Some of these packages of code can be imported straight into your Python code and others have to be downloaded and added before imported. One way of doing this is using a tool called PIP and you can use this Installing Python Modules with pip tutorial from The Programming Historian to have a go if you're new to it.

Many of the resources, tutorials, and courses listed on this page will use Python libraries. Which libraries to use depends on what you want to do. The website PyPI, or the Python Package Index, allows you to search for and browse different Python packages that can be added to and used for your code.

Everyone will have different projects and tasks in mind, and people will find different libraries more useful than others. Searching online for other people's suggestions for libraries to use or ways of doing things can be very useful, but bear in mind their project might differ to yours!

Further Python training and resources

Once you've tried out Python using the Getting started with coding in Python resources, you might want to learn more. Here are some useful resources and further courses to take.

Note: We haven't tested out all of these resources, so be critical with them to see if they cover what you want and suit how you want to learn. All of these resources are free or have a free version.

General introductory resources

w3schools is a good reference site for many coding languages, including Python. You can work through its Python tutorial, though it is focused on creating web applications and covers a lot of material very quickly. It is also good for checking how particular features work, which is why our session linked to pages from it.

FutureLearn has a range of free Python courses, mostly aimed at beginners. Some specific ones will be mentioned below, but it's a good place to look for different courses.

edX is another good site for browsing Python courses, where you can find ones like Microsoft's Introduction to Python: Absolute Beginner (or just search for 'python' and see what comes up).

Working with data

Software Carpentry has a range of lessons for computer-based research, including 'Programming with Python' which is used for our Introduction to programming with Python course. If you're not familiar with using a command line on your device, you'll want to start with their 'Unix Shell' lesson before the Python lesson(s). All the lessons and resources are available to use and download.

The Open University's free OpenLearn platform has various coding courses, including Learn to code for data analysis using Python. It uses Jupyter notebooks, which are similar to the Colab notebook used in our session.

Sites like edX and FutureLearn have courses created by universities and companies focused on data science, for example IBM's Python Basics for Data Science. If you've done the basics and want more, there's Harvard's courses including Using Python for Research or IBM's intermediate course Visualizing Data with Python.

If you're just interested in particular skills, then w3schools has sections of its Python tutorial dedicated to further Python elements like NumPy, a Python library for working with numerical data and arrays, machine learning, for getting programs to learn from data, and using Python with MySQL, so it works with databases.

If you're looking for in-depth written lessons on working with particular kinds of data, especially text, then the Programming Historian site is a good place to look. There's more on this under 'Working with text and other data' below.

Creating apps and games

The Raspberry Pi Foundation's courses on FutureLearn are a great way to learn Python the fun way. Aimed at teaching educators Python so they can teach it to students (but good for anyone to work through), these courses get you to make games and apps rather than work with data. Start with Programming 101 (or if you've used Scratch before, try their Scratch to Python course which includes working with a Pokemon API.

If you have to explore Python further and create a lot of small projects like different games and programs, then The Big Book of Small Python Projects, which can be read free online, has a huge range of projects to practice and learn more Python whilst making fun things.

Working with images and media

More interested in how you can use code for working with images, both creating and manipulating them? Again, the Raspberry Pi Foundation has a FutureLearn course on the topic, Representing data with images and sound, which provides an introduction to how computers work with and present images and sound and teaches Python for working with images.

For more intermediate resources on working with images in Python, see the N8 CIR YouTube channel's resources on understanding images with Python and analysing images with Python.

Working with text and other humanities data

There are a range of ways you can work with text data and other kinds of humanities data, and there are various resources for doing this. N8 CIR have resources introducing how Python can be used for the humanities.

The Programming Historian site has a range of Python lessons covering different humanities related research processes.

Some of the things you can learn how to do with Python are count word frequencies, work with text files, download web pages, and extract sets of keywords from a set of texts.

There are also more advanced lessons like Introduction to stylometry with Python, Exploring and Analyzing Network Data with Python, and Extracting Illustrated Pages from Digital Libraries with Python that you could explore.

Exercises

If you're not sure where to start with Python, the link below takes you to a practical workbook with exercises to build up some initial Python skills, building up to telling jokes and making chatbots..

Or you might want to try to work through the Introduction to Programming with Python course materials or check out another online course on Python.

Running Python using notebooks

Throughout this page we suggest using Google Colaboratory (Colab) notebooks for writing Python. These Colab notebooks allow you to write and run Python in your web browser without needing to set anything else up, making them very useful for getting started quickly and easily with Python.

Google have some resources, including a Welcome to Colaboratory and an overview of Colaboratory features that give an overview of what you can do with these notebooks.

Another option if you'd like to use a Python notebook but don't want to be using Python over the internet is to use Jupyter notebooks, which work very similarly. If you'd like to learn more about Jupyter notebooks in a lesson format, see Programming Historian's Introduction to Jupyter Notebooks.

Other ways of running Python

Python notebooks are one way of running Python and are particularly useful when you're first starting out or following a course that uses notebooks. However, they aren't the only way to run Python, and you'll probably want to move onto another way either instead of notebooks or using either dependent on your current project.

You can install Python on your device if it isn't already installed and then run Python using the command line or particular Python coding environments. The Beginner's Guide on the Python wiki has some information on doing this.

Anaconda is a popular open source way to run Python. It has a focus on data science and includes packages and tools for doing data science work with Python. Anaconda is installed on classroom PCs at the University of York and can be obtained from Software Centre on managed devices.

Doing data analysis?

If you're looking to do data analysis with coding and need some help with what methods you might use, which coding languages to try, or how to get your data into the right format/shape for analysis, see our Analysing Data Skills Guide.

Feedback
X