Working with Python Variables
Start course

This course offers an introduction to data science and looks at what a data scientist does. It then moves on to data science in Python and, through a range of guided walkthroughs, shows you how to use Python and its features. You will learn how to set up Anaconda and Jupyter Notebook and learn, using real-world examples, how to write Python code in Jupyter, with useful tips within the context of data science.

The course also looks at object-oriented planning, as well as Python variables and Python functions, and finally, it takes a look at Python data types and functions.

Learning Objectives

  • Understand data science and the role of data scientist
  • Set up Anaconda and Jupyter notebooks
  • Improve your knowledge of coding with Python
  • Understand how to work with Python variables, functions, and data

Intended Audience

This course is intended for:

  • Individuals looking for an introduction to data science
  • Those looking to enhance their knowledge of Python and its features


To get the most from this course, you should already have some knowledge of Python and programming languages in general.


Hello and welcome back. How can I store objects in memory in a retrievable fashion? I can assign them variables. So, an item and the assignment operator is the equals sign. I'm going to say, 'My name is Thomas'. Okay, and what this says is store the object 'Thomas' in memory and give it a reference name. A variable points to where it is in memory, it doesn't necessarily contain it. It just says that this is where it, it is in memory. So, if I want to know the actual memory address of the object, I could look at something like ID on name and what I get is the memory address for this object. This is in that slot. It's part of the reason why we actually want to use variable names because I don't want to have to reference that every single time I get this object out. I want to have a nickname essentially for every bit of data in memory. So, Python has protected keywords, so there are some things for which you cannot reassign, but you can do stupid things like reassign the string class to be the number nine, which means that now whenever I call str, I get the number nine out, but that's the name of the string class which has just broken Python a little bit there. Now, I'm still able to create things, it just means that now my pointer to the actual class definition of that is broken. Not everything that should be protected is protected but there are certain keywords, 'print', I can set print equal to five. So, if I want to call print now, print is the number five. 

If I want to call print on 'hello' or 'jello' then what I'm getting told is that I cannot use an integer to try and do this thing. So, you can break Python, I have never reassigned print before, but there are protected keywords, so I have something called 'def'. If I try to set def equal to zero, I get told off because def is always going to be used for function definition. I have things like 'true'. I cannot reassign the value of true in Python, so you know when you've got to protect it for a keyword, that keyword like 'value', when it's bold and green, when this is simply a built-in function object class, then it will not be bold green. Python is case sensitive, so I need to delete all of this because I've broken Python now. So, when you break Python, what you should do is you should delete what you've used to break it and then you should restart and run all cells and then everything should start working again. So, now, when I call str, what I get is str out. If I was to print out str, then I get told that this is the string class as opposed to the number that I set it to. So, it's semi-safe in that regard. So, yes, we have assignment operations, so the thing on the left-hand side of your assignment operation is generally something that's going to go on in memory. You're telling something's happened with regard to memory, whereas the thing on the right-hand side can be the result of something going on in the CPU.

So, I could call 'Thomas.upper.lower.capitalise' and store this in the variable name. I would perform all of these steps before I then stored it to memory under the variable name, 'name'. Okay, so, the right always evaluates before left, which isn't always true, but think it always does for now. Okay, so, if I run that now, then I should have name being, well, what is his name? Name will look exactly the same as before because I took it uppercase, lowercase and then capitalised the first letter. So, the object that I have been working with for all of these is called a string, strings are designed to, well, we can say they're designed to hold text information. That's a reasonable definition of what a string can be used for, they can be used to hold categorical data, they can be used for all sorts of things, like, they are one of the fundamental types of objects within Python. As a result, they can do things and the internal state of the string is whatever text it contains at that moment in time. Every string can be turned to uppercase. Every student can have certain things done. If I want to get information about how strings work, there is the inbuilt function which simply prints out the docstring. I can call help on the string class and this tells me how string works. All it is is printing out the help.string for this class. 

If I wanted to get help on a specific function, say it was upper and I can call help on string upper and this is going to print out what uppercase does and it returns a copy of the string converted to uppercase. So, this does nothing to the internal state of your string. It creates a new string with the transformation applied to it. If I want a slightly less friendly version of help, then that's where dir comes in. Dir, it will tell me everything. It will give you a list of everything associated with string and its namespace. This is the exact same as the help string except without any of the help. It's just a list of methods and attributes associated with my string object. What I would like us to do, a quick spot exercise-, we'll get a little bit comfy with Python and then we'll move on to speak about data types and operations, and all the extra stuff we can do.

About the Author

Delivering training and developing courseware for multiple aspects across Data Science curriculum, constantly updating and adapting to new trends and methods.

Covered Topics