Check out a free preview of the full Practical Guide to Python course
The "Data Types, Strings & Numbers Practice" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:
Nina walks the students through the data types, strings and numbers practice.
Transcript from the "Data Types, Strings & Numbers Practice" Lesson
[00:00:00]
>> Hi and welcome back. We're going to go over the practice for our basic data types, strings and numbers. Going back into our repple, Let's go ahead and start that from scratch so that we don't have any of the variables that we were. Using before making stuff up.
[00:00:28]
So in Python, we have integers. And we have floats and we have complex numbers and we can check the type on all of those to verify We also have strings. And lists. And the types for those are as you expect. We used helper functions on the ripple to help us diagnose, and solve problems.
[00:01:11]
So, type will let us know the type of variable or a value. We can also use dir, to figure out what methods are available for that particular type. And we can use help to either look at the help for that type or help is more useful when you pass in the specific method that you would like to know more about.
[00:01:39]
So for example, Stir is upper will tell me that this will return true if the string is uppercase and false otherwise, and help will also give us some hints as to what that method expects. Now, strings in Python are delineated either with. Single quotes or double quotes, but remember that the type of the quote has to match or that is a syntax error.
[00:02:17]
We can also pass in a string to print Or that's not a very good name. I'm going to change that variable to be Nina. If I can pass in a variable name here, i can also pass in multiple arguments to print for example name. And five and period and that will print out all of the arguments that I pass in with a space delineated.
[00:02:50]
Now we also have F strings. They start with the letter F. And when we have brackets in these F strings. We can put variables. So for example, name somebody in chat was very astute and figured out that you can also put any kinds of expressions in here. So let's say I had an expression like this I can go ahead and just put that between the curly braces.
[00:03:27]
So very useful shortcut. Let's say that I used that upper method from before. To uppercase my string. Again, remember that this doesn't actually change the value of the string, string is immutable. I could use that in combination with an f string. By calling that method inside of the string.
[00:03:57]
And again, just using this for output, not changing the value here. In Python, we also have lists. Remember, do not name your list, you'll have a bad time. As we saw earlier when I had a bad time when I named a variable list and completely forgot about it.
[00:04:22]
And then got weird syntax errors and had to think about it. So if we have a list called my list I can check it out in the repple. I can ask for its length And I can append values to it. So append, because I'm calling it on this list itself, and the list is mutable, meaning the list can be changed.
[00:05:10]
This is going to go ahead and change my list in place. I can get values by index. Remember lists have a 0 index. And that will go ahead and grab the individual items. I can change. Items in a list with assignment. So if I take a look at my list here, if I wanted to change the zero value to get the zero value.
[00:05:40]
I would just call that to change it, I would make an assignment to it. And now I've modified my list If we want to delete elements from a list. We could use the delete keyword. I would recommend not doing that. It's kind of confusing. So instead, I would advise you to call pop and you can call pop with an index as well.
[00:06:15]
So pop with no arguments just pop that last item, off the list if I wanted to save it at this point,I could assign it to a variable. And this operation has in fact changed my list. I can also sort my list in two ways. The first is with the sorted keyword.
[00:06:40]
The sorted keyword does not change the value of the list. But if I call my list.sort because I'm taking that action on a list which is a mutable. Type that's going to go ahead and modify the values inside. Does anyone have any questions before we move on to sets tuples and dictionaries.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops