[go: up one dir, main page]

0% found this document useful (0 votes)
21 views7 pages

List

Lists are a built-in Python data type used to store collections of data. Lists can contain elements of different data types and are ordered, changeable, and allow duplicate values. Lists use square brackets and commas to define elements, and support slicing, concatenation, and repetition like strings. Common list methods include append(), pop(), insert(), remove(), sort(), and len() to determine the length.

Uploaded by

patelhemv1143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

List

Lists are a built-in Python data type used to store collections of data. Lists can contain elements of different data types and are ordered, changeable, and allow duplicate values. Lists use square brackets and commas to define elements, and support slicing, concatenation, and repetition like strings. Common list methods include append(), pop(), insert(), remove(), sort(), and len() to determine the length.

Uploaded by

patelhemv1143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

List

Python Lists are similar to arrays in C. However, the list can contain data of different
types. The items stored in the list are separated with a comma (,) and enclosed within
square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator
(+) and repetition operator (*) works with the list in the same way as they were working
with the strings.

Consider the following example.

list1 = [1, "hi", "Python", 2]


#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
print (list1 * 3)

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:

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:

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:

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:


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:

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'>

What is the data type of a list?

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


print(type(mylist))

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)

Python Collections (Arrays)

There are four collection data types in the Python programming language:

 List is a collection which is ordered and changeable. Allows duplicate


members.
 Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
 Set is a collection which is unordered, unchangeable*, and unindexed.
No duplicate members.
 Dictionary is a collection which is ordered** and changeable. No
duplicate members.

*Set items are unchangeable, but you can remove and/or add items
whenever you like.

**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and
earlier, dictionaries are unordered.

When choosing a collection type, it is useful to understand the properties of


that type. Choosing the right type for a particular data set could mean
retention of meaning, and, it could mean an increase in efficiency or security.

List

Python Lists are similar to arrays in C. However, the list can contain data of different
types. The items stored in the list are separated with a comma (,) and enclosed within
square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator
(+) and repetition operator (*) works with the list in the same way as they were working
with the strings.

list1 = [1, "hi", "Python", 2]


#Checking type of given list
print(type(list1))
#Printing the list1
print (list1)
# List slicing
print (list1[3:])
# List slicing
print (list1[0:2])
# List Concatenation using + operator
print (list1 + list1)
# List repetation using * operator
print (list1 * 3)

Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection
of the items of different data types. The items of the tuple are separated with a comma
(,) and enclosed in parentheses ().

A tuple is a read-only data structure as we can't modify the size and value of the items
of a tuple.

tup = ("hi", "Python", 2)


# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
# Adding value to tup. It will throw an error.
t[2] = "hi"

Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can
modify after creation), and has unique elements. In set, the order of the elements is
undefined; it may return the changed sequence of the element. The set is created by
using a built-in function set(), or a sequence of elements is passed in the curly braces
and separated by the comma. It can contain various types of values. Consider the
following example.

# Creating Empty set


set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)

Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:

x = 1 # int
y = 2.8 # float
z = 1j # complex

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)

print(a)
print(b)
print(c)

print(type(a))
print(type(b))
print(type(c))

Random Number

Python does not have a random() function to make a random number, but
Python has a built-in module called random that can be used to make random
numbers:

Example

Import the random module, and display a random number between 1 and 9:

import random

print(random.randrange(1, 10))

In our Random Module Reference you will learn more about the Random
module.
Test Yourself Specify a Variable Type

There may be times when you want to specify a type on to a variable. This
can be done with casting. Python is an object-orientated language, and as
such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

 int() - constructs an integer number from an integer literal, a float


literal (by removing all decimals), or a string literal (providing the
string represents a whole number)
 float() - constructs a float number from an integer literal, a float
literal or a string literal (providing the string represents a float or an
integer)
 str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals

x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

You might also like