Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Quick List and Tuples Examples Python

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

# append() Adds an element at the end of the list

# clear() Removes all the elements from the list


# copy() Returns a copy of the list
# count() Returns the number of elements with the specified value
# extend() Add the elements of a list (or any iterable), to the end of the
current list
# index() Returns the index of the first element with the specified value
# insert() Adds an element at the specified position
# pop() Removes the element at the specified position
# remove() Removes the first item with the specified value
# reverse() Reverses the order of the list
# sort() Sorts the list

# random
# import random
# num = [1,1,5,5,9,9,7,55,55,456,456,123]
# winnerChoice = random.choice(num) #random
# winnerChoices = random.choices(num,k=3) #3 random
# winnerSample = random.sample(num,k=3) # 3 random without duplicate
# print(winnerChoice)
# print(winnerChoices)
# print(winnerSample)

# # gooing back in list


# # index starts at 0 forward
# # index starts at -1 backwards
# my_string = "roses are red"
# ch = my_string[0]
# ch1 = my_string[-1]
# print(ch)
# print(ch1)

# # looping in reverse
# for i in reversed(my_string):
# print(i)

# # slice list
# my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# midpoint = len(my_list) // 2
# start_index = 3
# end_index = 7
# first_half = my_list[:midpoint]
# second_half = my_list[midpoint:]
# slice_from_StartPoint = my_list[start_index:]
# slice_from_EndPoint = my_list[:end_index]

# print("First half:", first_half)


# print("Second half:", second_half)
# print("sliced Start:", slice_from_StartPoint)
# print("sliced End:", slice_from_EndPoint)

# # Split String into list


# my_string = "One Two Three Four"
# word_List = my_string.split()
# print(word_List)

# my_string = "1/2/3/4/5/6/7/8/9/10"
# word_List = my_string.split("/")
# print(word_List)

# # list ends
# a = [5,10,15,20,25]
# end = [a[0], a[len(a)-1]]
# print(end)

# #ex 13 list remove duplicates


# #using loops
# def listRemoverLoops(arr):
# y = []
# for i in arr:
# if i not in y:
# y.append(i)
# return y

# arr = [1,1,2,3,4,4,5,6,7,7,8,9,9,10,10]
# print(listRemoverLoops(arr))
# # remove duplicates using sets
# removeSet = set(arr)
# print(removeSet)

# # Truples Example
# # Trples are immutable (cannot be changed)
# # they fast asf too while having data integrity
# my_tuple = (1, 2, 3, 'a', 'b', 'c')

# print(my_tuple[0]) # Output: 1
# print(my_tuple[3]) # Output: 'a'

# print(my_tuple[2:5]) # Output: (3, 'a', 'b')

# # my_tuple[0] = 5 # This will raise a TypeError

# print(my_tuple.count('a')) # Output: 1
# print(my_tuple.index('b')) # Output: 4

# my_packed_tuple = 1, 2, 3 # Packing
# x, y, z = my_packed_tuple # Unpacking

# for item in my_tuple: #Loop through this bad boi


# print(item)

# print(len(my_tuple)) # Output: 6

# Loop in reverse many ways to do


# list_123 = [1,2,3,4,5,6,7,8,9,10]
# count = 1
# while count <= len(list_123):
# print(list_123[len(list_123)-count])
# count += 1

# # Better way to sort as it doesnt effect the original list


# num = [10,91,0,5,7,9,8,46,2,3,5,4]
# sorted_num = sorted(num)
# sortReverseNum = sorted(num,reverse=True)
# print(num)
# print(sorted_num)
# print(sortReverseNum)
# # Strings and loops with join NB
# courses = ['History','Math','Physics','IT','Motorboating']
# course_str =', '.join(courses)

# # Split the string to array


# new_list = course_str.split(", ")
# print(course_str)
# print(new_list)

You might also like