basics - Copy
basics - Copy
(Adapted from material developed by Profs. Milind Kulkarni, Stanley Chan, Chris
Brinton, David Inouye, and Qiang Qiu)
python basics
coding in python
• Standard Integrated Development Environments (IDEs)
• IDLE: Python’s own, basic IDE
• PyCharm: Code completion, unit tests, integration with git,
many advanced development features
(https://www.jetbrains.com/pycharm/)
• Spyder: Less plugins than PyCharm (not always a bad thing)
• Many more!
• Jupyter Notebook (https://jupyter.org/)
• Contains both computer code and rich text elements
(paragraphs, figures, …)
• Supports several dozen programming languages
• Very useful for data science development!
• You can download the notebook app or use Jupyter Hub
available on RCAC
(https://www.rcac.purdue.edu/compute/scholar)
• Anaconda package manager (https://www.anaconda.com/)
3
basic variables
• No "declaration" command as in other programming languages
• Variable is created when a value is assigned to it
• Can change type after they have been set
• Few rules on naming: Can make them very descriptive!
• Must start with a letter or underscore
• Case-sensitive (purdue & Purdue are different)
• Combinations (+) work on all types
“xyz ” + “abc” = “xyz abc”
3.2 + 1 = 4.2
4
operators and control statements
• Comparison operators: • Arithmetic operators:
a == b, a != b, a < b,
a + b, a - b, a * b,
a <= b, a > b, a >= b
a / b, a % b, a ** b, a//b
• If statement:
if r < 3: • Assignment operators:
print("x")
a = b, a += b, a -= b,
• If, elif, else (multiline blocks):
a *= b, a /= b, a **= b
if b > a:
print("b is greater than a") • Logical operators:
elif a == b:
print("a and b are equal") (a and b), (a or b),
else: not(a), not(a or b)
print("a is greater than b”)
5
lists
• One of the four collection data types • Length using len() method
• Also tuples, sets, and dictionaries print(len(thislist))
6
loops (more control statements)
• while loop: Execute while for x in range(5,10):
condition is true y = x % 2
i = 1
print(y)
while i < 6:
print(i) • break: Stop a loop where it is
i += 1 and exit
• for loop: Iterate over a • continue: Move to next
sequence iteration of loop
for x in “banana”: for val in “sammy_the_dog”:
print(x) if val == “h”:
• range() operator can be a break
useful loop iterator: print(val)
7
lists in for loops
• In other programming languages, for • Can also iterate through a list of lists
loop variables are integers data_list = [[1,2],[2,6],[5,7]]
for point in data_list:
• In Python, can use any ‘iterable’ object [x,y] = point
fruits = ["apple", "banana", "cherry"] z = x ** 2
for x in fruits: print(x,y,z)
if x == "banana":
• Can use the range function to iterate
continue
through integers
print(x)
for x in range(2, 30, 3):
adj = ["red", "big", "tasty"] • Can use a list to index another list
fruits = ["apple", "banana", "cherry"] ind = [1, 3, 5, 7]
for x in adj: values = [0] * 8
for y in fruits: for i in ind:
print(x, y) values[i] = i / 2
8
functions
• Block of code which runs when • To return a value, use the return
called statement
def my_function(x):
• Defined using def keyword
return 5 * x
def my_function():
print("Hello from a function”)
print(my_function(3))
• Call a function using its name print(my_function(5))
my_function()
• For multiple arguments, can use
• Parameters can be passed as keywords to specify order
input to functions def arithmetic(x,y,z):
def my_function(country): return (x+y)/z
print("I am from " + country)
print(arithmetic(z=3,x=2,y=4))
9
tuples
• Another of the four collection • Once a tuple is created, items cannot
data types be added or changed
• Tuples are ordered, • Workaround: Change to list, back to tuple
unchangeable, and allow • Check if item exists
duplicate members if "apple" in thistuple:
thistuple = print("Yes, 'apple' is in the
(“apple", "banana", “apple", “cherry”) fruits tuple")
• Indexed the same way as lists • Tuple with one item needs comma
thistuple = (“apple",) #Tuple
thistuple[0] => “apple"
thistuple = (“apple") #Not a tuple
thistuple[-1] => “cherry”
thistuple[1:3] => (“banana”,
• Built-in functions
thistuple.count(“apple")
“apple”) thistuple.index(“apple")
10
sets
• Collection which is unordered, (half) • Cannot change existing items, but can
changeable, and does not allow add and remove items
thisset.add(“orange")
duplicates
thisset.update(["orange", "mango",
• Written with curly brackets “grapes"])
thisset = {“apple”, "banana", thisset.remove("banana")
15
git illustration
16
git illustration
17