1. Home
  2. Training Library
  3. Big Data
  4. Courses
  5. Practical Data Science With Python

Working with Python Functions

Developed with
QA

The course is part of this learning path

Practical Data Science with Python
5
1
3
4
Start course
Overview
Difficulty
Intermediate
Duration
57m
Students
982
Ratings
3.7/5
starstarstarstar-halfstar-border
Description

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

Prerequisites

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

Transcript

Hello, and welcome back. Let's review the exercises from the lab. We have a few types of division in this exercise. Five divided by three gives us 1.7, as we would expect. Five double slash three gives us the whole number, after we've divided by three, so we can get three into five once, and that's all we care about. So, that's integer division, and then we have five percentage three, which tells us what the remainder is after we've done whole number division. So, I print each of these out. So, once we've divided by three, or once we divide five by three, we have two left over. If I divide it by six, or if I divided six by three rather, we would have nothing left over, so this is a divisibility test. If you perform modular division on a number, and if the result is zero, that means that the number on the left-hand side is divisible by the number on the right-hand side. So, we could use this as a test of evenness using percentage two. So, this is remainder division. So, the remainder after you divide six by two is zero, because it's fully divisible. Integer division means that this is how many threes we can get in five, and then this is true division. So, let's look at one plus true and the fourth power. So, one plus true to the fourth power, converted to a string, with the string three added to it, and then we want to take this entire object and convert it to a float, and then divide it by-, or use modular division, by three. So, you can do it all in one line. Python lets you do everything in one line as long as, again, you're aware of the results of every previous operation. 

 

So, we're using about, I don't know, four, five, six types in this operation, but as long as we're aware of what operation we're doing at each moment in time, it's fine. We can do it all in one line. We'll continue speaking about our variables types working with these things. I've assigned a few variables, there's nothing particularly baffling about this, some various variables at different types. Now, print is something called a variadic function. That's a good term to know, variadic function. What does it mean for something to be a variadic function? It means that I can add as many arguments or inputs to it as I like, and it will dynamically always print them all out. It takes in loads of stuff and it'll print out all that stuff to the screen. Unless I specify a specific named argument I want to alter, it's going to assume all of these things are going into it and actually print them out. We call that a variadic. If I want to concatenate strings together, I need to generally use the plus sign. There are methods that allow for string concatenation. What the plus sign requires is that the objects on either side of it are strings, so name, here is a string, this is a string that says 'is alive', and then the Boolean 'is alive' needs to be converted into a string before it can be concatenated and then printed out, but if I didn't cast this, it's an operation called casting, then I would get a good telling off from Python, telling me that I cannot concatenate a Boolean to a string. Let me give you another quick lesson right now. 

 

It's important to learn to read Python error messages. The big blob at the top generally points to where the error occurred, and the final line will tell you exactly what it's angry about. So, definitely learn to read these. The solution is just to read the message. What's it saying to you? And it will always tell you exactly what's gone wrong. This is just multi-type multiplication division, another string concatenation, this is a particularly useful thing, it's called an F string, simply a format string, and all that we've done is we've left holes in our string waiting to be filled up by variables that, at the time of creating this string, haven't been what we would call instantiated yet. So, what this ends up printing out is 'Thomas is 24 and 177cm', but I've simply outlined essentially a template that is going to be filled by my data, so I've got his name, his age and height times 100cm. So, once I've actually filled out these variables, then this string will be able to be printed out. If I didn't declare one of these, then I would get a telling off from Python, because it wouldn't have been able to find one of my variables. Another useful bit of string formatting is called raw strings, and I think it's probably worth mentioning them now. So, we have these things called raw strings, and why do we need them? Because there are special characters, and these things called escape characters, that can wreak havoc on your strings if you're not aware of them and you don't account for them. 

 

You will see havoc being wrought upon your strings by things like file paths. So, if I get the properties for this, if I copy the file path of this, there's a good chance that things are going to go a little bit wrong. And I try to print it out, I run this, then what happens is I get all this unicode error, unicode escape character, I couldn't figure out what to do with one of my escape characters. If I prepend this with an R then what happens is it views the string simply as the raw character elements within the string. A simple example, back slash R is a carriage return. If I print these out to the screen, and one, two, three, if I prepend this with an R, then I'm just going to get the actual raw input. So, with file paths, you always want raw string. When reading in from online sources and CSV files and all that sort of thing, you're always going to use a raw string, otherwise Python will start looking for things that aren't there. Okay, so, that's a raw string. So, we can combine F format strings with raw strings as well, so we'll format the string, and it will see it as a raw string once that's actually been carried out, so it'll format first and then view it raw after. So, these are useful bits of info to know. We have shortcut operations available to us as well, age plus existing age, plus one, minus age, plus equal to one, we can have age and star star equals two, and that should continually exponentiate age up to a certain point. 

 

And so what this is doing is saying you take age, do it to the power of two, and then reassign age, and it will just keep doing this again and again and again and again. So, eventually we're going to get-, ah, we're probably going to break the computer at this point, but we can perform these operations for pretty much anything, age divide equals two, age minus equals two. So, these are our various string methods. They differ in the fact that some of them take something in and some of them don't take anything in. These each perform an operation, so the operation of the top one is uppercase, the next is lowercase, the next asks if the string starts with a capital T, and it does, so we're getting a Boolean out, true, and then we ask if it ends with a capital T, and it doesn't, so we get another Boolean out as well, but in this case it's false. These are all actually functions carrying out different tasks. They are methods specifically, but at the root of it, they are functions.

About the Author
Avatar
Thomas Holmes
Data Science Trainer at QA Ltd.
Students
2115
Labs
2
Courses
5

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

Covered Topics