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

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

Strings in Python are immutable sequences of characters, while lists are mutable collections that can contain any data type. Strings can be converted to lists of characters using the list() function, but not vice versa. Additionally, global variables can be accessed throughout the program, while local variables are limited to their defined scope.

Uploaded by

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

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

Strings in Python are immutable sequences of characters, while lists are mutable collections that can contain any data type. Strings can be converted to lists of characters using the list() function, but not vice versa. Additionally, global variables can be accessed throughout the program, while local variables are limited to their defined scope.

Uploaded by

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