Python Tutorial
Python Tutorial
>>> spam = print('Hello!')
Hello!
>>> None == spam
True
Behind the scenes, Python adds return None to the end of any function
definition with no return statement. This is similar to how
a while or for loop implicitly ends with a continue statement. Also, if you
use a return statement without a value (that is, just the return keyword by
itself), then None is returned.
>> HelloWorld {bcs first print ends with ‘nothing’ instead of ‘newline’)
ANOTHER EG:
print('cats', 'dogs', 'mice', sep=',')
>>>cats,dogs,mice [comma used as separator instead of default ‘space’]
def bacon():
ham = 101
eggs = 0
spam()
It will return 99
>>> If a variable name is used both within the function and globally, then
both of them will behave individually without affecting each other However,
to avoid confusion, try naming them differently. For eg
def spam():
➊ eggs = 'spam local'
print(eggs) # prints 'spam local'
def bacon():
➋ eggs = 'bacon local'
print(eggs) # prints 'bacon local'
spam()
print(eggs) # prints 'bacon local'
➌ eggs = 'global'
bacon()
print(eggs) # prints 'global'
will return
bacon local
spam local
bacon local
global
>>> Now, to both “USE” & “MODIFY” the value of a global variable from
within a function itself, declare the variable as global within the function.
Now, the variable will act as if not within a function but globally. The
function will be able to use its global value as well as modify its global value.
>>> Suppose there’s a global variable, and you can use its value within a
function but you can’t modify the global variable without declaring it as
global first. Because the line of code that would modify its value would
actually create a local variable (since you haven’t declared it global).
For eg.
def spam():
print(eggs)
eggs = 42
spam()
eggs = 'global'
spam()
would return an error because the third line of code declares a local variable
named eggs and the 2nd line prints the value of a ‘local’ variable that has not
been assigned a value till that line of code. Hence, error.
x = range(3, 6)
for n in x:
print(n)
>>>3,4,5
On a side note, if statement is not a loop. For & while are loops. They
have functions such as continue (to start from beginning) and break (to
break out of the loop)
>>>NOTE : To continue Python codes in next line, end the line with a \
and continue typing in next line.
def spam(divideBy):
return 42 / divideBy
try:
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
except ZeroDivisionError:
print('Error: Invalid argument.')
the return is
21.0
3.5
Error: Invalid argument.
LISTS(useful):
spam[0] is ‘cat’
spam[-1] is ‘elephant’
spam[0:2] is [‘cat’,’bat’]
spam[0:-1] is [‘cat’,’bat’,’rat’]
spam[:2] is [‘cat’,’bat’]
Even a string is just a list of its individual characters. So, all these
functions would apply on a string as well. But unlike lists, strings are not
mutable, ie they cant be amended like lists. Only data can be extracted from
them.
Spam[1] = ‘hooter’
del spam[2]
Spam + [‘bat’,’loot’]
Print(spam) is ['cat', 'hooter', 'rat', 'elephant', ‘bat’,’loot’]
3)Misc
Now size is fat, color is gray and disposition is loud. This would work
only when the number of variables being assigned a value and len(list) is same,
otherwise an error will occur. If numbers are diff, then do like this
Size=cat[0]
4)Enumerate function
In its default sort, Big letters are placed before smaller letter. i.e A<a.
>List items can continue on next line. It wont hamper the code.
TUPPLES(useful):
Same as list except they are typed in () instead of []
And they are IMMUTABLE.
If you have only one value in your tuple, you can indicate this by placing a
trailing comma after the value inside the parentheses. Otherwise, Python will
think you’ve just typed a value inside regular parentheses. See yourself.
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
>>> spam = [0, 1, 2, 3, 4, 5]
➋ >>> cheese = spam # The reference is being copied, not the list.
➌ >>> cheese[1] = 'Hello!' # This changes the list value.
>>> spam
[0, 'Hello!', 2, 3, 4, 5]
>>> cheese # The cheese variable refers to the same list.
[0, 'Hello!', 2, 3, 4, 5]
Dictionaries(useful) :
message = 'It was a bright cold day in April, and the clocks were
striking
thirteen.'
count = {}
print(count)
Import pprint
Pprint.pprint(Dictionary) gives a better print view
A program to count total fruits brought by various
guests :