Skip to content

Instantly share code, notes, and snippets.

@pknowledge
pknowledge / Boolean, Comparison Operators and Logical Operators in Python
Last active September 1, 2018 19:00
Boolean, Comparison Operators and Logical Operators in Python
# Python Tutorial for Beginners 10 - Boolean, Comparison Operators and Logical Operators in Python
https://www.youtube.com/watch?v=0MBLkNlffT0
>>> True
True
>>> False
False
>>> true
Traceback (most recent call last):
File "<input>", line 1, in <module>
@pknowledge
pknowledge / hello.py
Created August 31, 2018 20:54
Python Tutorial for Beginners - If Else statements
# Python Tutorial for Beginners - If Else statements
x = -100
if x != 100 and x > 0:
print("x is = ")
print(x)
if x > 0:
print("x is positive")
else:
print("x is negative")
@pknowledge
pknowledge / hello.py
Created September 1, 2018 18:50
Python Tutorial for Beginners-if..elif..else statements + nested IF statements
# Python Tutorial for Beginners-if..elif..else statements + nested IF statements
#------------nested IF statements--------
x = 10
if x >= 0:
print("x is positive ")
if (x%2) == 0:
print("x is even")
else:
print("x is odd")
@pknowledge
pknowledge / Python_Lists.py
Created September 2, 2018 16:25
Python Lists
>>> x = [3, 5, 4, 9, 7, 10]
>>> x
[3, 5, 4, 9, 7, 10]
>>> x[0]
3
>>> x[4]
7
>>> y = ['max', 1, 15.5, [3, 2]]
>>> y[0]
'max'
@pknowledge
pknowledge / PythonTuples.py
Created September 2, 2018 19:20
Python Tuples
>>> x = (1,5,3,4,8)
>>>
>>> x
(1, 5, 3, 4, 8)
>>> x[0]
1
>>> x[4]
8
>>> x[100]
Traceback (most recent call last):
@pknowledge
pknowledge / Python_Sets.py
Last active September 3, 2018 13:19
Python Sets
>>> A = {1, 2, 5, 4, 7, 9, 2}
>>> A
{1, 2, 4, 5, 7, 9}
>>> len(A)
6
>>> A.add(10)
>>> A
{1, 2, 4, 5, 7, 9, 10}
>>> A.add(10)
>>> A
@pknowledge
pknowledge / Python_Dictionaries.py
Created September 3, 2018 16:39
Python Dictionaries
>>> D = {'name': 'max', 'age': 14, 'year': 2004}
>>> D
{'name': 'max', 'year': 2004, 'age': 14}
>>> D['name']
'max'
>>> D['age']
14
>>> E = {'name':'Tom', 15: 15, 15.1:15.1, True: True, (2,3): 5}
>>> E[(2,3)]
5
@pknowledge
pknowledge / python_slice.py
Created September 5, 2018 18:10
Python Slice
# Python Tutorial for Beginners - Python slice
# a[start:end] # items start through end-1
# a[start:] # items start through the rest of the array
# a[:end] # items from the beginning through end-1
# a[:] # a copy of the whole array
# a[start:end:step] # start through not past end, by step
# +---+---+---+---+---+---+
# | P | y | t | h | o | n |
# +---+---+---+---+---+---+
# 0 1 2 3 4 5
# Python Tutorial for Beginners - while Loop
#--------- Example 1 ---------------
i = 0
while True:
print("the value of i is : ", i)
i += 1 # i = i + 1
else:
print("Finished while loop")
#--------- Example 2 ---------------
@pknowledge
pknowledge / python_for_loop.py
Created September 6, 2018 14:25
Python For Loop
# Python Tutorial for Beginners - For Loop
print("For loop with Lists")
A = [0, 1, 2, 3, 4, 5] # list
for x in A:
print(x)
print("For loop with Tuples")
B = (0, 1, 2, 3, 4, 5) # tuple
for x in B:
print(x)