if __name__ == '__main__': # If file is not imported, this will be executed
main()$ python$ pip install <package-name>$ python <filename>.py$ time python <filename>.pyimport <filename>- Input
input("Input: ")- Output Python automatically points the cursor to a new line. We need not specify explicitly.
print("Output")In python, we need not specify the datatype of a variable. The interpreter interprets the value and assigns a suitabe datatype for that.
number = 0
org = "GitHub"In python, we do not write a block of code in a pair of paranthesis.
We write it after : followed by an indentation in the next line.
The conditional statements include if, if-else, nested if and so on...
x,y = 0,1
if x < y:
print("x is less than y")
else:
print("x is not less than y")Note that the colon (:) following is required.
Similarly, the nested if also works.
As other programming languages, we have
for loop
for i in range(5):
print(i)The range function starts off with 0 till the number(excluded).
while loop
i=0
while(i < 10):
print("{} is less than 10".format(i))
i += 1There are a few ways to format a string in Python.
- Using the
%operator Strings can be formatted using the % operator:
>>> foo = 'world'
>>> 'Hello %s' % foo
'Hello world'To subsitute multiple instances, wrap the right hand side in a Tuple:
>>> foo = 'James'
>>> bar = 'Nancy'
>>> 'Hi, my name is %s and this is %s' % (foo, bar)
'Hi, my name is James and this is Nancy'You can also do variable subsitutions with a dictionary:
>>> dict = { "name": "Mike", "country": "Canada" }
>>> 'I am %(name)s and I am from %(country)s' % dict
'I am Mike and I am from Canada'.format()
Introduced in Python 3, but is available in Python 2.7+
>>> 'Hello {}'.format('world')
'Hello world'Similar to the above, subsitutions can be referred by name:
>>> 'Hi {name}, your total is ${total}'.format(name='Bob', total=5.50)
'Hi Bob, your total is $5.5'Available in Python 3.6+. Works similar to the above, but is more powerful as arbitrary Python expressions can be embedded:
>>> a = 5
>>> b = 10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'More information: See the section on f-strings in this Playbook.
Function is a block of code which runs when it is called.
Functions are declared using the def keyword. Function name must be a valid identifier.
Function arguments can be literal values, variables (valid identifiers), and expressions.
def sum(a, b) :
return a + b
def subtract(a, b) :
return a - b
def getPerson(name, age) :
person = { "name": name, "age": age }
return personFunctions can be called by passing the arguments according to the declaration.
a = 20
b = 50
c = sum(a, b)
d = sum(b, 50)
e = subtract(b, a)
p = getPerson("Joe", 25)
# OUTPUT:
print( "Sum - {} plus {}: {}" . format( a, b, c ) ) # Sum - 20 plus 50: 70
print( "Sum - {} plus 50: {}" . format( b, d ) ) # Sum - 50 plus 50: 100
print( "Subtraction - {} minus {}: {}" . format( b, a, e ) ) # Subtraction - 50 minus 20: 30
print( "Person - {}" . format( p ) ) # Person - {'name': 'Joe', 'age': 75}All data in a Python is represented by objects. There’s nothing particularly special about functions. They’re also just objects.
def yell(text): # Define function yell
return text.upper() + '!'
>>> bark = yell # Declare an object "bark" that contains the function "yell"
>>> bark('woof') # You could now execute the "yell" function object by calling bark
'WOOF!'Functions can be defined inside other functions. These are often called nested functions or inner functions.
def speak(text): # Define function speak
def wisper(t): # Function wisper does not exist outside speak
return t.lower() + '...'
return wisper(text)
>>> speak('Hello, World')
'hello, world...'The lambda keyword in Python provides a shortcut for declaring small anonymous functions.
>>> add = lambda x, y: x + y
>>> add(5, 3)
8You could declare the same add function with the def keyword, but it would be slightly more verbose:
def add(x, y):
return x + y
>>> add(5, 3)
8# These are all inplace operations returns a None value
<list>.append(<ele>) # Add an element to the end of the list
<list>.sort() # Sorts the given list
<list>.pop([<ele>]) # Removes the last element if no argument else removes the element at the index given
<list>.clear() # Makes it an empty list
<list>.insert(<index>, <ele>) # Adds the element before the index
<list>.extend(<iterator>)
<list>.reverse() # Reverse a given list# These are not inplace operations and has a return value
<list>.copy() # Makes a shallow copy of the list
<list>.index(<ele>) # Returns the index of the given element
<list>.count(<ele>) # Returns the number of occurrences of the elementkey-value pairs.
<dict> = {'Google':100, 'Facebook':80, 'Apple':90}
<dict>['Amazon'] = 85 # Adding a key along with the value
# Accessing the dictionary
for key in <dict>:
print("{key} -> {x}".format(key=key, x=<dict>[key]))
<dict>.keys() # Print all the keys
<dict>.values() # Print all the values
len(<dict>) # Find the length of the dictionary
<dict>.pop(<key>) # Removes the item with the specified key name
<dict>.copy() # Make a copy of a dictionaryA dictionary can also contain many dictionaries, this is called nested dictionaries.
A double asterisk ** denotes dictionary unpacking. Its operand must be a mapping. Each mapping item is added to the new dictionary. Later values replace values already set by earlier dict items and earlier dictionary unpackings.
Originally proposed by PEP 448.
- Define the Function
def make_sandwich(protein, cheese, sauce, extras=None):
"""Prints the ingredients of a customized sandwich."""
sandwich = f"A {protein} sandwich"
if cheese:
sandwich += f" with {cheese}"
if sauce:
sandwich += f" and {sauce} on it"
if extras:
sandwich += f" (plus {extras})"
print(sandwich)- Define the Configuration Dictionary
We create a dictionary where the keys match the keyword argument names of the function (protein, cheese, sauce, etc.).
# Our sandwich configuration stored in a dictionary
sandwich_settings = {
'protein': 'turkey',
'cheese': 'provolone',
'sauce': 'mayo',
'extras': 'lettuce and tomato'
}- Use the Unpacking Operator (
**)
We call the function and use ** to unpack the dictionary. The ** operator turns the dictionary's key-value pairs into individual keyword arguments.
# Unpack the dictionary into keyword arguments
make_sandwich(**sandwich_settings)Output:
A turkey sandwich with provolone and mayo on it (plus lettuce and tomato)
What the ** Does
The line make_sandwich(**sandwich_settings) is functionally equivalent to writing:
make_sandwich(
protein='turkey',
cheese='provolone',
sauce='mayo',
extras='lettuce and tomato'
)The ** operator is a clean way to pass a dynamic or pre-configured set of arguments to a function.
A tuple is a collection which is ordered, indexed and unchangeable. In Python tuples are written with round brackets.
this_tuple = ('books', 'pen', 'paper') # Defined a tuple
# Accessing Tuple Items
print(this_tuple[2]) # paperTuples are immutable, which means they cant to changed once they are created.
If a value inside tuple needs to be changed, the tuple must be converted to a list.
Newly created list can be converted back to tuple after updating changes.
desk_tuple = ("pen stand", "plant", "marker")
desk_list = list(desk_tuple)
desk_list[2] = "highlighter"
desk_tuple = tuple(desk_list)
print(desk_tuple[2]) # highlighterTo create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.
this_tuple = ("Python",)
print(type(this_tuple)) # tuple
# NOT a tuple
this_tuple = ("Python")
print(type(this_tuple)) # strTuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:
this_tuple = ('books', 'pen', 'paper')
del this_tuple
print(this_tuple) # ERROR: this_tuple is not defined