Skip to content

Instantly share code, notes, and snippets.

@akaomy
Last active November 12, 2018 00:43
Show Gist options
  • Save akaomy/c181620d62e261136f60537a2f020b76 to your computer and use it in GitHub Desktop.
Save akaomy/c181620d62e261136f60537a2f020b76 to your computer and use it in GitHub Desktop.
list = list()
list = list([2,3,4])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'list' object is not callable
list = []
list.append(2)
list.append(3, 4)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
list.append(3, 4)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
list.append(3)
list.append(4)
list
[2, 3, 4]
list = ['b', 'c', 'd']
list
['b', 'c', 'd']
if 'b' in list:
print("yay")
yay
if 'v' not in list:
print("yay")
yay
list2 = [2,3,4]
print(list + list2)
['b', 'c', 'd', 2, 3, 4]
list2
[2, 3, 4]
list2 * 2
[2, 3, 4, 2, 3, 4]
2 * list2s
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'list2s' is not defined
list[0]
'b'
list[0:1]
['b']
list[1:2]
['c']
len(list)
3
min(list2)
2
min(list)
'b'
list2
[2, 3, 4]
list
['b', 'c', 'd']
max(list)
'd'
sum(list)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
sum(list2)
9
list2
[2, 3, 4]
for i in list2:
print(i)
2
3
4
list < list2
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
list < atr(list2)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'atr' is not defined
list
['b', 'c', 'd']
list3 = [5, 3, 2]
list2 > list3
False
list2 => list3
File "<input>", line 1
list2 => list3
^
SyntaxError: invalid syntax
list2 >= list3
False
list2 <= list3
True
list2 < list3
True
list2 != list3
True
list2 = list3
del list3
list2
[5, 3, 2]
list3 = [[1,2,3], [3,4], [5,6,7,8]]
list3
[[1, 2, 3], [3, 4], [5, 6, 7, 8]]
for i in list3:
print(len(i))
3
2
4
import random
print(random.shuffle(list3))
None
print(random.shuffle(list2))
None
print(random.shuffle(list))
None
v = random.shuffle(list3)
print(v)
None
print(type(v))
<class 'NoneType'>
list
['c', 'b', 'd']
list2
[2, 3, 5]
list2.append(3)
random.shuffle(list2)
random.shuffle(list2)
random.shuffle(list2)
random.shuffle(list2)
random.shuffle(list2)
print(list2)
[3, 5, 2, 3]
random.shuffle(list2)
print(list2)
[3, 5, 3, 2]
# Print odd numbers:
for i in range(0, len(list2), 2):
print(list2[i])
# List comprehensions:
list1 = [x for x in range(5)]
list1
[0, 1, 2, 3, 4]
list2 = [0.5 * x for x in list1]
list2
[0.0, 0.5, 1.0, 1.5, 2.0]
list3 = [x for x in list2 if x < 1.5]
list3
[0.0, 0.5, 1.0]
# Splitting a string into a list
items = "Jane John James Jack"
print(items.split())
['Jane', 'John', 'James', 'Jack']
items = "09/20/2012".split("/")
items
['09', '20', '2012']
# creates empty list where user can insert any number one by one
for i in range(10):
list4.append(eval(input()))
1
2
3
4
5
6
7
8
9
10
list4
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
s = input("Enter 5 numbers separated by s[aces from one line: ")
Enter 5 numbers separated by s[aces from one line: >? 1 2 3 4 5
s
'1 2 3 4 5'
items = s.split() # extract items from the string
lst = [eval(x) for x in items] # convert ['1', '2', '3', '4', '5'] to numbers
lst
[1, 2, 3, 4, 5]
def shift(lst):
temp = lst[0] # Retain the first element
# Shift elements left
for i in range(1, len(lst)):
lst[i - 1] = lst[i]
# Move the first element to fill in the last position
lst[len(lst) - 1] = temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment