Check out a free preview of the full Python Fundamentals course

The "Practice: Data Types" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to display the type of several variables in their REPL. Nina live codes the solution to the exercise.

Preview
Close

Transcript from the "Practice: Data Types" Lesson

[00:00:04]
>> Nina Zakharenko: So if you open chapter two,
>> Nina Zakharenko: You'll see an exercise on basic data types, strings and numbers. This is a pretty short exercise, so let's aim for ten minutes. And before we kick it off, just a reminder that if you see code here that says Python REPL, you can hit this copy icon to copy it to the clipboard.

[00:00:31]
So just to cover a question that came up earlier. For NVS code, if you were not able to open code by typing code space dot in your terminal from where your virtual environment was activated in the project folder, you might have to explicitly select your interpreter. And as long as you have the Python extension for VS Code, you should see a blue bar here at the bottom, you can hit this.

[00:01:01]
If you already opened your VS Code with code dot, you don't have to do this. But if you didn't, you would open, click on that, and the select the right interpreter. Generally, it would be this format, ./n, that's the virtual environment, /bin/python3. On Windows, it might be something else, but you'll see kind of the list of available options.

[00:01:28]
And you wanna make sure that the version is right, because the first result here is Python 2.7, and we don't want that. Okay, so going through our basic data types, strings and numbers, just for shorthand, I'm not going to name my variables here, I'm just gonna pass them directly into type.

[00:01:51]
So the type of 42, that's an int, the type of 3 divided by 4, it's a float. If I made a new int this way, and one of the cool things about the int function is we don't have to just pass integers into it, we can also pass in a string with an integer, and that will work just fine.

[00:02:19]
So my type of z is an int, it's a kind of a handy shortcut. I can also do that with a float, so if I pass an integer into a float, and I check the type of a, it'll be of class float, just to take a quick peek at it, it's 5.0.

[00:02:38]
Okay, so numbers, we wanna calculate the amount of rent that we pay daily by taking our monthly rent and dividing it by 30. So let's say I live in San Francisco and I pay $5,000 for a studio apartment. I don't, I live in Portland. But the per day amount would just be the rent divided by 30.

[00:03:05]
Now, we could just type in our variable here and see the result, or we can use the built-in function print to explicitly print out the value. You won't need to use print much on the REPL, if at all. But once we move on to real Python programs, that's how we're gonna see what's contained in variables.

[00:03:33]
>> Nina Zakharenko: So if I printed out the value of Hello World, I would see that. If I set my name to Nina, I could also print out My name is. And here's a kind of really fun quick shorthand for the print statement. If you want to print a bunch of things separated by space, you can pass them separated by comma to the print function.

[00:04:01]
So if you don't wanna type an f string necessarily in this case. So this is gonna print out my name is, notice the space, and Nina.
>> Nina Zakharenko: Or if I wanted to do the same thing with an f string, and I can also pass the f string to print.

[00:04:20]
Remember that f string starts with a lowercase f. And I could say My name is,
>> Nina Zakharenko: And then do the curly braces, and the name of the variable, which is name.
>> Nina Zakharenko: So a quick example here for percent formatting, just in case you kind of see it out in the wild.

[00:04:45]
If I have my name Nina, I could print out Hello, and then I would put in %s to designate that the value that I want to interpolate here is a string, followed by %, followed by name. So a lot less readable. And as soon as you start passing multiple values into this, it gets even less readable, so that's why it's not the recommended standard.

[00:05:16]
>> Nina Zakharenko: Okay, so we had,
>> Nina Zakharenko: We had our name, which is Nina. You can also do some kind of basic operations in f strings, which is pretty cool. So for this case, I'm just gonna not use the print because it's kind of optional in the REPL. I'm just gonna say Hello, my name is Nina.

[00:05:41]
I pay,
>> Nina Zakharenko: I'm gonna put a dollar sign here, just to signify the dollar amount, not a special symbol or anything in this case. I'm gonna say I'm gonna pay rent divided by 30 in rent, per day.
>> Nina Zakharenko: Okay, it's a lot of money. And quickly, just going through our helper functions.

[00:06:14]
We talked about type,
>> Nina Zakharenko: Type of x is int. We talked about dir, so if I do a dir on int, remember, it has to be the class here, I'll see all the methods available on int. The ones without the underscores are the ones that we care about.

[00:06:36]
And I can also,
>> Nina Zakharenko: Use the help function. Let's say I wanted to know more about what this does. I can do int., int is the type, followed by the method, and pass that into help, and it'll print out the documentation. I can press Q to exit.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now