Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Problems solving and python programming

Rating
-
Sold
-
Pages
12
Uploaded on
06-01-2024
Written in
2023/2024

It is useful for learning list in python programming

Institution
Course

Content preview

Python Lists
mylist = ["apple", "banana", "cherry"]

List
 Lists are used to store multiple items in a single variable.
 Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are Tuple, Set, and Dictionary, all
with different qualities and usage.
 Lists are created using square brackets:

Example
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List Items
 List items are ordered, changeable, and allow duplicate values.
 List items are indexed, the first item has index [0], the second item
has index [1] etc.
Ordered
 When we say that lists are ordered, it means that the items have a
defined order, and that order will not change.
 If you add new items to a list, the new items will be placed at the
end of the list.

Note: There are some list methods that will change the order, but in
general: the order of the items will not change.

Changeable
The list is changeable, meaning that we can change, add, and remove
items in a list after it has been created.

Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Example
Lists allow duplicate values:

thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

List Length
To determine how many items a list has, use the len() function:




1

, Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))

List Items - Data Types
List items can be of any data type:

Example
String, int and boolean data types:

list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

A list can contain different data types:
Example
A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]

type()
From Python's perspective, lists are defined as objects with the data type
'list':
<class 'list'>

Example
What is the data type of a list?

mylist = ["apple", "banana", "cherry"]
print(type(mylist))

Output
<class ‘list’>

The list() Constructor
It is also possible to use the list() constructor when creating a new list.

Example
Using the list() constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-
brackets
print(thislist)

Output
2

Connected book

Written for

Course

Document information

Summarized whole book?
Yes
Uploaded on
January 6, 2024
Number of pages
12
Written in
2023/2024
Type
SUMMARY

Subjects

$20.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
rithikmillert

Get to know the seller

Seller avatar
rithikmillert
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions