Thumbnail for حق الرد.. | عالي المقام مع علاء الحطاب by قناة الاولى العراقية - Alawla TV

حق الرد.. | عالي المقام مع علاء الحطاب

قناة الاولى العراقية - Alawla TV

8m 47s1,169 words~6 min read
YouTube auto captions
Transcript source

YouTube auto captions

This transcript was extracted from YouTube's auto-generated caption track. The transcript below is server-rendered so it can be read, searched, cited, and shared without opening the original YouTube player.

Pull quotes
[0:00]We've talked about Python lists and a Python tuple is very similar to a Python list.
[0:00]The main difference is that a tuple is immutable, meaning that once you define a tuple, you cannot change the values inside of it.
[0:00]Here we're defining a tuple called my tuple and we're assigning it the values 1, 2, 3 in parentheses separated by commas.
[0:00]Now if we print out the value of my tuple, we can see that it prints out 1, 2, 3 just like we would expect.
Use this transcript
Related transcript hubs

[0:00]Hello and welcome to another video from The Coding Institute. Today we're going to be talking about Python tuples. Now, what are Python tuples? We've talked about Python lists and a Python tuple is very similar to a Python list. The main difference is that a tuple is immutable, meaning that once you define a tuple, you cannot change the values inside of it. A list on the other hand is mutable. That's the main distinction between a Python list and a Python tuple. So let's talk about how to define a tuple in Python. Defining a tuple is very similar to defining a list. You define a tuple by putting items in parentheses separated by commas. So let's take a look at an example here. Here we're defining a tuple called my tuple and we're assigning it the values 1, 2, 3 in parentheses separated by commas. Now if we print out the value of my tuple, we can see that it prints out 1, 2, 3 just like we would expect. And if we check the type of my tuple using the type function, we can see that it's of class tuple. Now, what if we want to define an empty tuple? We can define an empty tuple just by putting open and close parentheses. So here, we're defining my tuple 2 as an empty tuple. And if we print out the value of my tuple 2, we can see that it's just open and close parentheses. And if we check the type of my tuple 2, we can see that it's of class tuple. Now, what if we want to define a tuple with a single item? Well, you might think you would just put the item in parentheses like we have here in this example. My tuple 3 equals 1 in parentheses. And if we print out the value of my tuple 3, it just prints out 1, just like we would expect. But if we check the type of my tuple 3, we can see that it's of class int, not tuple. And the reason for that is because Python interprets putting something in parentheses as just an operation that changes the order of operations. So in this example here, my tuple 3 equals 1 in parentheses. It's just saying my tuple 3 equals 1. It's not defining a tuple. So if you want to define a tuple with a single item, you need to add a comma after the item. So as you can see in this example here, we're defining my tuple 4 as one comma in parentheses. So if we print out the value of my tuple 4, we can see that it prints out one comma just like we'd expect. And if we check the type of my tuple 4, we can see that it's of class tuple. So remember, if you want to define a tuple with a single item, you need to add a comma after the item inside the parentheses. So tuples support indexing just like lists do. You can access items in a tuple using their index. So in this example here, we have my tuple five as one, two, three. And if we print out my tuple five at index zero, it prints out one. If we print out my tuple five at index one, it prints out two. If we print out my tuple five at index two, it prints out three. You can also access items in a tuple using negative indexing, just like with lists. So if we print out my tuple five at index negative one, it prints out three. If we print out my tuple five at index negative two, it prints out two. If we print out my tuple five at index negative three, it prints out one. Now, remember, tuples are immutable, meaning you cannot change the values inside of them. So if we define my tuple six as one, two, three. And then we try to reassign my tuple six at index zero to the value four. It's going to throw a type error, because tuples don't support item assignment. So now let's talk about tuple concatenation. You can concatenate two tuples by using the addition operator. So in this example here, we have tuple one defined as one, two, three, and tuple two defined as four, five, six. And if we concatenate tuple one and tuple two, it creates a new tuple with the values one, two, three, four, five, six. You can also use tuple multiplication to create a new tuple that repeats the original tuple multiple times. So in this example here, we have my tuple seven defined as one, two, three. And if we multiply my tuple seven by two, it creates a new tuple with the values one, two, three, one, two, three. You can also check if an item is in a tuple using the in operator. So in this example here, we have my tuple eight defined as one, two, three. And if we check if one is in my tuple eight, it prints true. If we check if four is in my tuple eight, it prints false. You can also iterate over a tuple using a for loop, just like with lists. So in this example here, we have my tuple nine defined as one, two, three. And if we iterate over my tuple nine and print each item, it prints one, then two, then three. So let's talk about some common tuple methods. The count method returns the number of times a specified value appears in the tuple. So in this example here, we have my tuple ten defined as one, two, two, three. And if we call the count method on my tuple ten for the value two, it returns two because two appears twice in the tuple. The index method finds the first occurrence of a specified value and returns its index. So in this example here, we have my tuple eleven defined as one, two, three, two. And if we call the index method on my tuple eleven for the value two, it returns one because the first occurrence of two is at index one. So let's talk about unpacking tuples. You can unpack a tuple by assigning its elements to multiple variables. So in this example here, we have my tuple twelve defined as one, two, three. And if we assign x, y, z to my tuple twelve, then x will be one, y will be two, and z will be three. So if we print out x, y, z, it prints out one, two, three. So that's a quick overview of Python tuples. Remember, the main difference between a tuple and a list is that a tuple is immutable, meaning once you define it, you cannot change the values inside of it. A list on the other hand is mutable. Thank you for watching this video. Please like and subscribe for more content.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript