![]() ![]() Note that you didn’t write append() as a standalone function, as you would use print(), say. ![]() The value 5000 was added to the end of the list. Let’s remember how you can use append(): > many_numbers = When you created a list and then you wanted to add items to the list you used append(). You’ve also already seen one method being used in the previous Chapter. Functions can have arguments in the parentheses, and they return data when they complete their actions. They are written in lowercase letters and have parentheses at the end. You’ve seen print() and range() are functions. This description will make a lot more sense with some examples. A method is a function that is associated with a specific data type and acts on that data type. If you know what a function is, and you do from the previous Chapter, you also know what a method is. Here’s a new term you haven’t encountered so far: method. In the next section, you’ll see how understanding the difference between mutable and immutable data types can help explain other behaviour that may seem odd at first glance. You’re not modifying an existing string, but you’re discarding the old string and replacing it with a new one that you assign to the same variable name. In this case, you have reassigned new data to the variable my_name. You can even use the data structure directly, for example: for item in "Stephen": As long as the function returns an iterable data type, it can be used directly in the for loop statement. You’ve already seen how you can use a function such as range(). In the for loop statement, you don’t necessarily need to use a variable name at the end of the line. You’re reusing the same box to save having to get a new box! When the computer program reaches the second loop, it will overwrite whatever is already in the variable item with the new values. This is fine as long as you only need to use this variable within the loop. ![]() You may have noticed that you used the variable item in both of the for loops. So the variable item will contain the letter "S" in the first iteration of the for loop, then t in the second iteration, and so on. When you iterate through a string, each character is considered one at a time. ![]() You’ll get no errors saying that these data types are not iterable in this case: S You can experiment with the data types you’ve encountered so far, for example: a_number = 5 You’ll often hear of the first iteration or the second iteration of a loop, say, to refer to one of the repetitions.Ī data type is iterable if Python can go through the items in the data type one after the other, such as in a for loop. When you repeat a block of code several times using a for loop, you are iterating the block of code. Cannot-be-changed doesn’t sound as grand as immutable! My aim in this book is the opposite, but we cannot escape using the terms you’ll find in documentation, error messages and other texts. Like in every other profession, programmers like to use complex-sounding words to make the subject look difficult and elusive. In this section, you’ll learn about sequences, iterables, mutable and immutable data types. This is because some data types may have similar properties, and when you know how to deal with a category of data type, you’ll be better equipped to deal with a new data type you’ll encounter from the same category. Many, many more!Īs you learn about more data types, it is helpful to learn about the categories of data types. There are many more data types in Python. You’ve used integers and floats, strings, Booleans, and lists. You’ve already come across several of the most important data types in Python. Can you guess the five most common words in Pride and Prejudice? You’ll find out the answer when you complete the project later on in this Chapter. In the second part, you’ll work on a new project in which you’ll work out all the words that Jane Austen used when writing Pride and Prejudice and how often she used each word. The first part of this Chapter will cover some of the theory related to data types. In this Chapter, you’ll read about how data types are handled in Python and about the different categories of data types. Languages such as Python provide many alternatives for you to consider. And if you’re using a programming language, you’ll want to perform some actions with the data and transform it in one way or another.ĭeciding how you want your program to store the data is an important decision you’ll need to make as a programmer. The data could be a simple name or number as in the Angry Goblin game, or you could have a very complex and highly structured data set. No computer program can exist without data. And then you need to do something with that information. You need information in any computer program. Granted, this is not the most detailed and technical definition you’ll find, but it summarises a program perfectly. ![]()
0 Comments
Leave a Reply. |