Basic Python Training at Arcesium - Day 1

Apr 15-18, 2019 Vikrant Patil

These notes are available online at http://notes.pipal.in/2019/arcesium_basic_apr/day1.html

© Pipal Academy LLP

Day 1 | Day 2 | Day 3 | Day 4

We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from

https://www.anaconda.com/download/

# Header1 #
## Header2 ##

Header1

Header2

In [1]:
1 + 1
Out[1]:
2

Basic data types in python

**integers**

integers

In [2]:
2 - 3
Out[2]:
-1
In [3]:
2 * 3
Out[3]:
6
In [4]:
2 ** 3
Out[4]:
8
In [5]:
2 ** 1000 # power
Out[5]:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
In [6]:
5 / 2 #division operator
Out[6]:
2.5
In [7]:
5 // 2  #integer division
Out[7]:
2
In [8]:
5 % 2 # mod
Out[8]:
1

floats

In [9]:
1.6 *3
Out[9]:
4.800000000000001
In [10]:
1.6 - 1
Out[10]:
0.6000000000000001
In [11]:
1.5**5
Out[11]:
7.59375

string

In [12]:
"hello" + " " + "world"
Out[12]:
'hello world'
In [13]:
'hello' + ' ' + "arcesium"
Out[13]:
'hello arcesium'
In [14]:
first = "Vikrant"
last = "Patil"
In [15]:
print(first)
Vikrant
In [16]:
first
Out[16]:
'Vikrant'

Rules for variable names

  • Name can not have space, -, + , *, . not even comma
  • It can have alphabets (caps and small both allowed) , numbers, _ (underscore)
  • it can not start with number
In [17]:
x = "text data"
x1 = x + x
x2 = "another text"
In [18]:
x
Out[18]:
'text data'
In [19]:
x1
Out[19]:
'text datatext data'
In [20]:
x2
Out[20]:
'another text'
In [21]:
x + x1
Out[21]:
'text datatext datatext data'
In [22]:
star = "*"
In [23]:
fivestar = star*5 #it repeates the string 5 times
In [24]:
fivestar
Out[24]:
'*****'
In [25]:
multiline = "first line\nsecond line"
In [26]:
print(multiline)
first line
second line
In [27]:
poem = """
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
"""
In [28]:
poem1 = '''
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.

'''
In [29]:
print(poem)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.

In [30]:
print(poem1)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.


In [31]:
n = 2
In [32]:
n
Out[32]:
2
In [33]:
print(n)
2
In [34]:
multiline
Out[34]:
'first line\nsecond line'
In [35]:
print(multiline)
first line
second line
In [36]:
dialog = 'he said, "I am fine!"'
In [37]:
print(dialog)
he said, "I am fine!"
In [38]:
statement = "it's his style"
In [39]:
print(statement)
it's his style
In [40]:
multi =  "first line\nsecond line"
multi1 = """first line
second line"""
In [41]:
print(multi)
first line
second line
In [42]:
print(multi1)
first line
second line
In [43]:
dialog1 = "he said, " '"he' "'s" ' way of doing thigs!"'
In [44]:
print(dialog1)
he said, "he's way of doing thigs!"
In [45]:
"first" "second" "third"
Out[45]:
'firstsecondthird'
In [46]:
x x1 x2
  File "<ipython-input-46-d2045c3c47a2>", line 1
    x x1 x2
       ^
SyntaxError: invalid syntax
In [47]:
2x = 2*x
  File "<ipython-input-47-e1ec61d45331>", line 1
    2x = 2*x
     ^
SyntaxError: invalid syntax
In [48]:
empty = ""

Lists

In [49]:
digits = [0, 1,2,3,4,5,6,7,8, 9]
In [50]:
digits[0]
Out[50]:
0
In [51]:
digits[9]
Out[51]:
9
In [52]:
digits[-1]
Out[52]:
9
In [53]:
digits[-2]
Out[53]:
8

---->    0   1   2   3
data    'A' 'B' 'C' 'D'
        -4  -3  -2  -1   <-----
In [54]:
digits
Out[54]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [55]:
digits
Out[55]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [77]:
digits[0] = -1 # lists allow modification of data
In [57]:
digits
Out[57]:
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [58]:
chars = ["A","B", "C"]
In [59]:
chars
Out[59]:
['A', 'B', 'C']
In [60]:
mixed = [1, 2, 3, 1.2, "string2", "hello"]
In [61]:
mixed[-1]
Out[61]:
'hello'
In [62]:
col1 = [1,2,3,4]
col2 = ["A","B","C","D"]
col3 = [1, 4, 9, 16]
table = [col1, col2, col3]
In [63]:
table
Out[63]:
[[1, 2, 3, 4], ['A', 'B', 'C', 'D'], [1, 4, 9, 16]]
In [65]:
table[0] # 0th column
Out[65]:
[1, 2, 3, 4]
In [66]:
table[-1] # last column
Out[66]:
[1, 4, 9, 16]
In [67]:
table[1]
Out[67]:
['A', 'B', 'C', 'D']
In [70]:
table[0][0] #0th column , 0th row
Out[70]:
1
In [71]:
table[0][-1] #0th column , last row
Out[71]:
4

tuples sibling of list

In [72]:
point = (1, 0, 5.6) #(x, y, z) coordinates
In [73]:
point[0]
Out[73]:
1
In [74]:
point[-1]
Out[74]:
5.6
In [78]:
point[1] = 1 # tuples do not allow modification of data
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-78-1d9af4b57f74> in <module>
----> 1 point[1] = 1 # tuples do not allow modification of data

TypeError: 'tuple' object does not support item assignment
In [81]:
addpoint = point + point # concatenates
In [82]:
addpoint
Out[82]:
(1, 0, 5.6, 1, 0, 5.6)
In [80]:
point
Out[80]:
(1, 0, 5.6)
In [84]:
(1, 1, 0)*5 # repeates it five times
Out[84]:
(1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0)

dictionaries

In [85]:
person = {"name":"Vikrant", "email":"vikrant@example.com", "company":"Pipal Academy"}
In [86]:
person['name']
Out[86]:
'Vikrant'
In [87]:
person['email']
Out[87]:
'vikrant@example.com'
In [88]:
person['launguage']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-88-b36d48b90310> in <module>
----> 1 person['launguage']

KeyError: 'launguage'
In [89]:
digits[12]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-89-ce7dad302b67> in <module>
----> 1 digits[12]

IndexError: list index out of range
In [90]:
company_profile = {"name":"HCL", "ticker":"hcl", "value":1001.0, "exchange":"BSE"}
In [91]:
company_profile['name']
Out[91]:
'HCL'
In [92]:
company_profile['value']
Out[92]:
1001.0
In [93]:
company_profile['value'] = 1002.3
In [94]:
print(company_profile)
{'name': 'HCL', 'ticker': 'hcl', 'value': 1002.3, 'exchange': 'BSE'}

sets sets are collection which is unique. no repetaion allowed

In [95]:
digits = {1, 2, 3, 4, 6, 6, 7}
In [96]:
digits
Out[96]:
{1, 2, 3, 4, 6, 7}

boolean

In [97]:
yes = True
In [98]:
no = False
In [105]:
yes
Out[105]:
True
In [106]:
no
Out[106]:
False
In [99]:
1 + 2
Out[99]:
3
In [100]:
x
Out[100]:
'text data'
In [101]:
x1
Out[101]:
'text datatext data'
In [103]:
x == x1 #equality operator checks if two items are same
Out[103]:
False
In [107]:
inttypes = {True:"even", False:"odd"}
In [108]:
inttypes[True]
Out[108]:
'even'

Dictionaries can have hashable keys (int, boolean, string, tuple) and any value.

nothing

In [104]:
nothing = None

Functions

Functions are predefined actions which may need some inputs. So these actions can be called again again and with different inputs

In [109]:
digits = [0,1,2,3,4,5,6,7,8,9]
In [110]:
len(digits) # len is called as function. digits is input for this function
Out[110]:
10
In [111]:
items = ["A","B","C"]
In [112]:
len(items)
Out[112]:
3
In [115]:
len(point) # it can work for tuples, gives length of tuple
Out[115]:
3
In [117]:
len(company_profile) # it can work for dictionary, gives length of dictionary, number of keys
Out[117]:
4
In [119]:
len(poem) # number of charecters in string..includes all charecters like space, tab, new line
Out[119]:
224
In [121]:
"2" # this is a string
Out[121]:
'2'
In [122]:
"2" + 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-122-192a1d19ca7a> in <module>
----> 1 "2" + 2

TypeError: can only concatenate str (not "int") to str
In [123]:
two = "2"
In [125]:
int(two) # int is a function which  converts text into integer
Out[125]:
2
In [126]:
float("1.2") # float is a function which  converts text into float
Out[126]:
1.2
In [127]:
str(100) # str converts the object to string format
Out[127]:
'100'
In [128]:
str(digits)
Out[128]:
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
In [129]:
sum([1,2,3,4,5,6])
Out[129]:
21
In [130]:
sum([1.1,2.3, 3.5])
Out[130]:
6.9
In [132]:
sum(1, 2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-132-67d1519cc1ff> in <module>
----> 1 sum(1, 2)

TypeError: 'int' object is not iterable
In [133]:
sum([1,2,3,4])
Out[133]:
10
In [134]:
len("2")
Out[134]:
1
In [135]:
len(2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-135-49e19d3e86d2> in <module>
----> 1 len(2)

TypeError: object of type 'int' has no len()
In [136]:
len(digits)
Out[136]:
10
In [137]:
len(point)
Out[137]:
3
In [138]:
len()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-138-adf3103c7c3e> in <module>
----> 1 len()

TypeError: len() takes exactly one argument (0 given)
In [139]:
len = 5
In [140]:
len(digits)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-140-9b7d253b44df> in <module>
----> 1 len(digits)

TypeError: 'int' object is not callable
In [141]:
del len
In [142]:
len(digits)
Out[142]:
10

problem

  • Find number of digits in 2**100
In [143]:
2**100
Out[143]:
1267650600228229401496703205376
In [144]:
x = 2**100
In [146]:
len(str(x))
Out[146]:
31
In [149]:
sx = str(x)
len(sx) # these two statements are same as above statement
Out[149]:
31

custom functions

In [150]:
def add(a, b):
    s = a + b
    return s
In [151]:
add(2, 4)
Out[151]:
6
In [152]:
add("hello", "world")
Out[152]:
'helloworld'
In [153]:
def say_hello(p):
    print("Hello " + p)
In [154]:
say_hello("Arcesium")
Hello Arcesium
In [155]:
def line():
    print("*"*30)
In [156]:
line()
******************************
In [157]:
x = add(5,6)
In [158]:
x
Out[158]:
11
In [159]:
s = say_hello("Arcesium")
Hello Arcesium
In [160]:
s
In [161]:
print(s)
None
In [162]:
say_hello("Hyderabad")
Hello Hyderabad
In [163]:
city = "Pune"
In [165]:
say_hello(city)
Hello Pune
In [166]:
say_hello(pune)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-166-acbc45550c7e> in <module>
----> 1 say_hello(pune)

NameError: name 'pune' is not defined

problem

  • Write a function count_digits which takes an integer as input and returns number of digits in that integer
    >>> count_digits(100)
    3
In [167]:
def count_digits(n):
    sn = str(n)
    return len(sn)
In [169]:
count_digits(2**100)
Out[169]:
31
In [170]:
count_digits(2**1000)
Out[170]:
302
In [171]:
count_digits("234434")
Out[171]:
6
In [172]:
add(3,4)
Out[172]:
7
In [173]:
add(3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-173-4c015eca70a5> in <module>
----> 1 add(3)

TypeError: add() missing 1 required positional argument: 'b'
In [174]:
add(1, 2, 3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-174-30a43e0f0f21> in <module>
----> 1 add(1, 2, 3)

TypeError: add() takes 2 positional arguments but 3 were given
In [175]:
{1, 2, 3, 4} + {1, 5, 6}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-175-7750ea8aa668> in <module>
----> 1 {1, 2, 3, 4} + {1, 5, 6}

TypeError: unsupported operand type(s) for +: 'set' and 'set'
In [176]:
s = {1,1,1,2,3,34,54,1,2,3,3}
In [177]:
s
Out[177]:
{1, 2, 3, 34, 54}
In [178]:
s = {54, 34, 2, 2, 1, 1}
In [179]:
s
Out[179]:
{1, 2, 34, 54}
In [181]:
def twice(x):
    return 2*x

def twice_(x):
    print(2*x)
In [182]:
twice(4)
Out[182]:
8
In [183]:
twice_(4)
8
In [184]:
twice(twice(4))
Out[184]:
16
In [185]:
twice_(twice_(4))
8
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-185-3199b6f9dbf3> in <module>
----> 1 twice_(twice_(4))

<ipython-input-181-e77584a89e94> in twice_(x)
      3 
      4 def twice_(x):
----> 5     print(2*x)

TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
In [186]:
range(0,10)
Out[186]:
range(0, 10)
In [187]:
list(range(0,10))
Out[187]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [189]:
list(range(10))
Out[189]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [190]:
list(range(1,10))
Out[190]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [191]:
list(range(1,20,3))
Out[191]:
[1, 4, 7, 10, 13, 16, 19]
In [199]:
def naturals(n):
    return range(1,n+1)
In [200]:
list(naturals(20))
Out[200]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
In [201]:
def sum_naturals(n):
    return sum(naturals(n))
In [202]:
sum_naturals(100)
Out[202]:
5050

Functions as first class objects

In [203]:
def square(x):
    return x*x
In [204]:
square
Out[204]:
<function __main__.square(x)>
In [205]:
square(4)
Out[205]:
16
In [206]:
square
Out[206]:
<function __main__.square(x)>
In [207]:
x = 1
In [208]:
y = x
In [210]:
y
Out[210]:
1
In [209]:
sqr = square
In [211]:
sqr
Out[211]:
<function __main__.square(x)>
In [212]:
sqr(4)
Out[212]:
16
In [213]:
square(4)
Out[213]:
16
In [214]:
del square
In [215]:
square(4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-215-ea6c1ff999ad> in <module>
----> 1 square(4)

NameError: name 'square' is not defined
In [216]:
sqr(4)
Out[216]:
16
In [226]:
def sqaure(x):
    return x*x

def sumofsquares(x, y):
    return sqaure(x) + sqaure(y)
In [227]:
def cube(x):
    return x*x*x

def sumofcubes(x, y):
    return cube(x) + cube(y)
In [228]:
def sumof(x,y, func):
    return func(x) + func(y)
In [229]:
sumofsquares(2, 3)
Out[229]:
13
In [230]:
sumof(2, 3, sqaure)
Out[230]:
13
In [231]:
sumof(2, 3, cube)
Out[231]:
35
In [232]:
sumofcubes(2, 3)
Out[232]:
35
In [233]:
max([2,34, 545,12,12,1])
Out[233]:
545
In [234]:
words = ["one", "two", "three", "four", "five"]
In [235]:
words
Out[235]:
['one', 'two', 'three', 'four', 'five']
In [236]:
max(words)
Out[236]:
'two'
In [237]:
max(words , key=len)
Out[237]:
'three'
In [240]:
records = [
    ("HCL", 800.5, 2),
    ("HAL", 55.54, 5.4),
    ("INFY",1000.4, -4),
    ("TATA", 100, 1.3),
    ("RELIANCE", 707, 3)
]
## 0th column is name
## 1st column is value
## 2nd column is gain
In [239]:
max(records)
Out[239]:
('TATA', 100, 1.3)
In [ ]:
def get_value(row):
    return row[1]

def get_gain(row):
    return row[2]
In [241]:
max(records, key= get_value)
Out[241]:
('INFY', 1000.4, -4)
In [242]:
max(records, key=get_gain)
Out[242]:
('HAL', 55.54, 5.4)
In [243]:
min(records, key=get_gain)
Out[243]:
('INFY', 1000.4, -4)
In [244]:
sorted(records)
Out[244]:
[('HAL', 55.54, 5.4),
 ('HCL', 800.5, 2),
 ('INFY', 1000.4, -4),
 ('RELIANCE', 707, 3),
 ('TATA', 100, 1.3)]
In [245]:
sorted(records, key=get_value)
Out[245]:
[('HAL', 55.54, 5.4),
 ('TATA', 100, 1.3),
 ('RELIANCE', 707, 3),
 ('HCL', 800.5, 2),
 ('INFY', 1000.4, -4)]
In [246]:
add
Out[246]:
<function __main__.add(a, b)>
In [247]:
add(2,3)
Out[247]:
5
In [248]:
sorted(records, key=get_value, reverse=True)
Out[248]:
[('INFY', 1000.4, -4),
 ('HCL', 800.5, 2),
 ('RELIANCE', 707, 3),
 ('TATA', 100, 1.3),
 ('HAL', 55.54, 5.4)]
In [249]:
help(sorted)
Help on built-in function sorted in module builtins:

sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

In [250]:
records = [
    {"name":"HCL", "value":800.5, "gain":2},
    {"name":"HAL", "value":55.54, "gain":5.4},
    {"name":"INFY","value":1000.4, "gain":-4},
    {"name":"TATA", "value":100, "gain":1.3},
    {"name":"RELIANCE", "value":707, "gain":3}
]
In [252]:
def get_value(r):
    return r['value']
max(records, key=get_value)
Out[252]:
{'name': 'INFY', 'value': 1000.4, 'gain': -4}

Methods

In [253]:
text = "Lets have THIS sentence as random text"
In [254]:
text
Out[254]:
'Lets have THIS sentence as random text'
In [255]:
type(text)
Out[255]:
str
In [256]:
text.lower()
Out[256]:
'lets have this sentence as random text'
In [257]:
text.upper()
Out[257]:
'LETS HAVE THIS SENTENCE AS RANDOM TEXT'
In [258]:
text
Out[258]:
'Lets have THIS sentence as random text'
In [259]:
text.split(" ")
Out[259]:
['Lets', 'have', 'THIS', 'sentence', 'as', 'random', 'text']
In [260]:
text.split() 
Out[260]:
['Lets', 'have', 'THIS', 'sentence', 'as', 'random', 'text']
In [261]:
multi = """
text       with \t
lots of      white    spaces
"""
In [262]:
multi.split(" ")
Out[262]:
['\ntext',
 '',
 '',
 '',
 '',
 '',
 '',
 'with',
 '\t\nlots',
 'of',
 '',
 '',
 '',
 '',
 '',
 'white',
 '',
 '',
 '',
 'spaces\n']
In [263]:
multi.split()
Out[263]:
['text', 'with', 'lots', 'of', 'white', 'spaces']
In [264]:
def make_words(text):
    return text.split()
In [265]:
make_words(poem)
Out[265]:
['The',
 'Zen',
 'of',
 'Python,',
 'by',
 'Tim',
 'Peters',
 'Beautiful',
 'is',
 'better',
 'than',
 'ugly.',
 'Explicit',
 'is',
 'better',
 'than',
 'implicit.',
 'Simple',
 'is',
 'better',
 'than',
 'complex.',
 'Complex',
 'is',
 'better',
 'than',
 'complicated.',
 'Flat',
 'is',
 'better',
 'than',
 'nested.',
 'Sparse',
 'is',
 'better',
 'than',
 'dense.']
In [266]:
text.lower()
Out[266]:
'lets have this sentence as random text'
In [267]:
text.upper()
Out[267]:
'LETS HAVE THIS SENTENCE AS RANDOM TEXT'
In [268]:
text.capitalize()
Out[268]:
'Lets have this sentence as random text'
In [270]:
text.lower().count("e")
Out[270]:
6
In [272]:
trailing =  "   dasdasdas asdasdasd   sdasd   "
In [273]:
tailing.strip()
Out[273]:
'dasdasdas asdasdasd   sdasd'
In [275]:
hello_template = "Hello Mr. NAME"
In [276]:
hello_template.replace("NAME", "Amit")
Out[276]:
'Hello Mr. Amit'
In [277]:
text.startswith("lets")
Out[277]:
False
In [279]:
text.lower().startswith("lets")
Out[279]:
True
In [280]:
text
Out[280]:
'Lets have THIS sentence as random text'
In [281]:
text.endswith("xt")
Out[281]:
True
In [282]:
filesname = "Hello.py"
In [283]:
def is_python(name):
    return name.endswith(".py")
In [284]:
is_python(filesname)
Out[284]:
True
In [285]:
is_python("windows.exe")
Out[285]:
False
In [287]:
text.center(50)
Out[287]:
'      Lets have THIS sentence as random text      '
In [288]:
text.ljust(50)
Out[288]:
'Lets have THIS sentence as random text            '
In [289]:
text.rjust(50)
Out[289]:
'            Lets have THIS sentence as random text'
In [290]:
words
Out[290]:
['one', 'two', 'three', 'four', 'five']
In [291]:
" ".join(words)
Out[291]:
'one two three four five'
In [292]:
"_".join(words)
Out[292]:
'one_two_three_four_five'
In [293]:
"\\".join(["c:", "programs", "anaconda"])
Out[293]:
'c:\\programs\\anaconda'

List methods

In [294]:
ones = [1, 1, 1, 1]
In [295]:
ones.append(0)
In [296]:
ones
Out[296]:
[1, 1, 1, 1, 0]
In [297]:
ones.append(10)
In [298]:
ones
Out[298]:
[1, 1, 1, 1, 0, 10]
In [299]:
ones.remove(10)
In [300]:
ones
Out[300]:
[1, 1, 1, 1, 0]
In [301]:
ones.pop()
Out[301]:
0
In [302]:
ones
Out[302]:
[1, 1, 1, 1]
In [303]:
ones.append(-1)
In [304]:
ones
Out[304]:
[1, 1, 1, 1, -1]
In [306]:
ones.pop(0) # pop 0th element
Out[306]:
1
In [307]:
ones.remove(0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-307-8c7fb13780d5> in <module>
----> 1 ones.remove(0)

ValueError: list.remove(x): x not in list
In [308]:
ones
Out[308]:
[1, 1, -1]
In [309]:
ones.remove(1)
In [310]:
ones
Out[310]:
[1, -1]
In [311]:
ones.extend([1, 1, 1, 0, 0])
In [312]:
ones
Out[312]:
[1, -1, 1, 1, 1, 0, 0]
In [313]:
x = [1, 1, 1]
In [314]:
x + [1,2,3]
Out[314]:
[1, 1, 1, 1, 2, 3]
In [315]:
x
Out[315]:
[1, 1, 1]
In [316]:
digits
Out[316]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [317]:
numbers = [3,7,2,9,3,89,37,1]
In [318]:
sorted(numbers)
Out[318]:
[1, 2, 3, 3, 7, 9, 37, 89]
In [319]:
numbers
Out[319]:
[3, 7, 2, 9, 3, 89, 37, 1]
In [320]:
numbers.sort()
In [321]:
numbers
Out[321]:
[1, 2, 3, 3, 7, 9, 37, 89]
In [322]:
numbers.reverse()
In [323]:
numbers
Out[323]:
[89, 37, 9, 7, 3, 3, 2, 1]
In [324]:
numbers.index(7)
Out[324]:
3
In [ ]: