Python: 7/20/2018 1 Python by Varun Arora
Python: 7/20/2018 1 Python by Varun Arora
▪ A comment may appear at the start of a line or following whitespace or code, but not within a string literal.
▪ Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in
examples.
▪ Example:
▪ Example
x=5
y = "John"
print(x)
print(y)
▪ Variables do not need to be declared with any particular type and can even change type after
they have been set.
▪ Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
▪ You can also use the + character to add a variable to another variable:
▪ Example
x = "Python is "
y = "awesome"
z= x+y
print(z)
▪ If you try to combine a string and a number, Python will give you an error:
▪ Example
x=5
y = "John"
print(x + y)
▪ Variables of numeric types are created when you assign a value to them:
▪ Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
▪ To verify the type of any object in Python, use the type() function:
▪ Example
print(type(x))
print(type(y))
print(type(z))
print(type(x))
print(type(y))
print(type(z))
print(type(x))
print(type(y))
print(type(z))
print(type(x))
print(type(y))
print(type(z))
print(type(x))
print(type(y))
print(type(z))
▪ Integers:
▪ x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
▪ Example
▪ Floats:
▪ Example
▪ Strings:
▪ There are four collection data types in the Python programming language:
▪ List is a collection which is ordered and changeable. Allows duplicate
members.
▪ Tuple is a collection which is ordered and unchangeable. Allows duplicate
members.
▪ Set is a collection which is unordered and unindexed. No duplicate members.
▪ Dictionary is a collection which is unordered, changeable and indexed. No
duplicate members.
▪ Example
– Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
▪ Example
Using the remove() method to remove an item:
thislist = list(("apple", "banana", "cherry"))
thislist.remove("banana")
▪ Example
The len() method returns the number of items in a list:
thislist = list(("apple", "banana", "cherry"))
print(len(thislist))
▪ Example
Return the item in position 1:
thistuple = ("apple", "banana", "cherry“)
print(thistuple[1])
▪ Set
▪ A set is a collection which is unordered and unindexed. In Python sets are
written with curly brackets.
▪ Example
– Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
▪ Note: the set list is unordered, so the items will appear in a random order.
▪ Removing Items
– Removing a dictionary item must be done using the del() function in python:
▪ Example
thisdict = dict(apple="green", banana="yellow", cherry="red")
del(thisdict["banana"])
print(thisdict)
▪ Indentation
– Python relies on indentation, using whitespace, to define scope in the code. Other
programming languages often use curly-brackets for this purpose.
▪ The elif keyword is pythons way of saying "if the previous conditions were not
true, then do this condition".
▪ Example
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
▪ While Loop
– With the while loop we can execute a set of statements as long as a condition is true.
▪ Example
– Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1
NOTE: The for loop does not require an indexing variable to set beforehand, as the for
command itself allows for this.
▪ Example
def my_function():
print("Hello from a function")
▪ Calling a Function
– To call a function, use the function name followed by parenthesis:
▪ Example
▪ def my_function():
print("Hello from a function")
my_function()
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
▪ Note: Default value can be given to last parameter.
print(my_function(3))
print(my_function(5))
print(my_function(9))
▪ Lambda defined functions can have more than one defined input, as shown here:
▪ Example
myfunc = lambda x,y: x*y
print(myfunc(3,6))
doubler = myfunc(2)
tripler = myfunc(3)
val = 11
print("Doubled: " + str(doubler(val)) + ". Tripled: " + str(tripler(val)))
▪ Example
– Delete the age property from the p1 object
del p1.age
▪ Delete Objects
– You can delete objects by using the del keyword
▪ Example
– Delete the p1 object
del p1
▪ To create a module just save the code you want in a file with the file extension .py:
▪ Example
– Save this code in a file named mymodule.py
▪ def greeting(name):
print("Hello, " + name)
▪ When using a function from a module, use the syntax: module_name.function_name.
mymodule.greeting("Jonathan")
▪ When using a function from a module, use the syntax:
module_name.function_name.
a = mx.person1["age"]
print(a)
x = platform.system()
print(x)
▪ There is a built-in function to list all the function names (or variable names) in
a module. The dir() function:
▪ Example
– List all the defined names belonging to the platform module:
import platform
x = dir(platform)
print(x)
▪ Example
– Import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])