0% found this document useful (0 votes)
2 views

What are some important differences between a string and a list in Python

The document outlines key differences between strings and lists in Python, highlighting that strings are immutable and can only contain characters, while lists are mutable and can hold any data type. It also explains global and local variables, emphasizing the scope of global variables and the limited scope of local variables. Additionally, the document introduces the concept of classes and objects in Python through examples, illustrating polymorphism with a Cat and Dog class.

Uploaded by

aniiiiiiket
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

What are some important differences between a string and a list in Python

The document outlines key differences between strings and lists in Python, highlighting that strings are immutable and can only contain characters, while lists are mutable and can hold any data type. It also explains global and local variables, emphasizing the scope of global variables and the limited scope of local variables. Additionally, the document introduces the concept of classes and objects in Python through examples, illustrating polymorphism with a Cat and Dog class.

Uploaded by

aniiiiiiket
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

What are some important differences between a string and a list in Python?

Inheritance in Python Global and Local Variables in Python?


Strings and lists share many similarities as we have seen throughout this lesson. # Parent class 1
However, strings are not interchangeable with lists because of some important Global variables
class Person:
differences. def person_info(self, name, age): A global variable can be used anywhere in the program as its scope is the entire
Strings can only consist of characters, while lists can contain any data type. print('Inside Person class') program. Let’s understand global variable with a very simple example-
Because of the previous difference, we cannot easily make a list into a string, but we print('Name:', name, 'Age:', age) z = 25 Output: 25, 20
can make a string into a list of characters, simply by using the list() function. # Parent class 2 def func():
message = "Hello" class Company: global z
message_to_list = list(message) def company_info(self, company_name, location): print(z)
print(message_to_list)
print('Inside Company class') z=20
# message_to_list is now ['H', 'e', 'l', 'l', 'o']
print('Name:', company_name, 'location:', location) func()
Furthermore, when we add to a string, we use the + concatenation operator to do
# Child class print(z)
so. With a list, we use the .append() method to add elements.
class Employee(Person, Company): Local variables
Finally, one of the most important differences is mutability. Strings are immutable,
def Employee_info(self, salary, skill): Local variables can only be reached within their scope(like func() above). Like in
meaning that we cannot update them. We could update values in a list however
quite easily. print('Inside Employee class')
below program- there are two local variables – x and y.
# This will result in this error message: print('Salary:', salary, 'Skill:', skill)
def sum(x,y):
# TypeError: 'str' object does not support item assignment # Create object of Employee
sum = x + y
name = "Joe" emp = Employee()
return sum
name[0] = "P" # access data
emp.person_info('Jessa', 28) print(sum(5, 10))
# For lists, we can easily update an element
name_as_list = ["J", "o", "e"] emp.company_info('Google', 'Atlanta')
Python Tuple
name_as_list[0] = "P" emp.Employee_info(12000, 'Machine Learning')
A tuple in Python is similar to a list. The difference between the two is that we
# name_as_list is now ["P", "o", "e"] output: - inside Person class
cannot change the elements of a tuple once it is assigned whereas we can
Inside Person class change the elements of a list.
Name: Jessa Age: 28 Creating a Tuple
explain the concept of classes and objects using an example in Python? Inside Company class A tuple is created by placing all the items (elements) inside parentheses () ,
Name: Google location: Atlanta separated by commas. The parentheses are optional, however, it is a good
Inside Employee class practice to use them.
Salary: 12000 Skill: Machine Learning # Different types of tuples
# Empty tuple
Polymorphism in Class Methods my_tuple = ()
class Cat: print(my_tuple)
def __init__(self, name, age): # Tuple having integers
self.name = name my_tuple = (1, 2, 3)
self.age = age print(my_tuple)
def info(self): # tuple with mixed datatypes
print(f"I am a cat. My name is {self.name}. my_tuple = (1, "Hello", 3.4)
I am {self.age} years old.") print(my_tuple)
# nested tuple
def make_sound(self): my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print("Meow")
print(my_tuple)
class Dog:
output:-
def __init__(self, name, age):
()
self.name = name
(1, 2, 3)
self.age = age
(1, 'Hello', 3.4)
def info(self):
print(f"I am a dog. My name is {self.name}. ('mouse', [8, 4, 6], (1, 2, 3))
I am {self.age} years old.")
def make_sound(self):
print("Bark")
cat1 = Cat("Kitty", 2.5)
dog1 = Dog("Fluffy", 4)
for animal in (cat1, dog1):
animal.make_sound()
animal.info()
animal.make_sound()
Output:-
Meow
I am a cat. My name is Kitty. I am 2.5 years old.
Meow
Bark
I am a dog. My name is Fluffy. I am 4 years old.
Bark

You might also like