1.python Basics Edx
1.python Basics Edx
Background on Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
Python for Data Science
Python Overview
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
(Ju) Julia
(Pyt) Python
(R) R
Python for Data Science
https://www.python.org/about/
Python for Data Science
Variables in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
printf("Hello\n");
}
Java
public class Hi {
public static void main (String [] args) {
System.out.println("Hello");
}
}
C python
#include "stdio.h" print("hello")
int main() {
Python for Data Science
printf("Hello\n");
Notice: no ;
}
Java
public class Hi {
public static void main (String [] args) {
System.out.println("Hello");
}
}
C python
#include "stdio.h" x = 3
y = 4
Python for Data Science
int main() {
int x = 3;
x = 4.5;
}
int main() {
int x = 3;
x = 4.5;
}
Dynamic Typing!!
Python for Data Science
Objects in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
Dynamic Typing!!
python
x = 3 PyIntObject {
value;
Python for Data Science
x PyIntObject {
value = 3
}
python
x = 3 PyFloatObject {
x = 4.5 value;
Python for Data Science
x PyIntObject {
value = 3
}
PyFloatObject {
value = 4.5
}
python
x = 3 PyFloatObject {
x = 4.5 value;
Python for Data Science
x PyIntObject {
value = 3
}
PyFloatObject {
value = 4.5
}
Python shell
>>> x = 3 is returns if the references
>>> y = 3.0 point to the same object
Python for Data Science
>>> x is y
x PyIntObject {
value = 3
}
y PyFloatObject {
value = 3.0
}
Python shell
>>> x = 3 is returns if the references
>>> y = 3.0 point to the same object
Python for Data Science
>>> x is y
False
x PyIntObject {
value = 3
}
y PyFloatObject {
value = 3.0
}
Python shell
>>> x = 3 is returns if the references
>>> y = 3.0 point to the same object
Python for Data Science
x PyIntObject {
value = 3
}
y PyFloatObject {
value = 3.0
}
Python shell
>>> x = 3 is returns if the references
>>> y = 3.0 point to the same object
Python for Data Science
x PyIntObject {
value = 3
}
y PyFloatObject {
value = 3.0
}
Feel free to try this yourselves in python
using shell
Python for Data Science
Python for Data Science
Objects in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
x PyStringObject {
value = "Hello"
}
python
>>> x = "Hello"
Python for Data Science
https://docs.python.org/2/library/stdtypes.html#string-methods
python
>>> x = "Hello"
>>> x.lower() <var_name>.<method_name>(params)
Python for Data Science
'hello'
python
>>> x = "Hello"
>>> x.lower()
Python for Data Science
'hello'
>>> x
'Hello'
python
>>> x = "Hello"
Python for Data Science
x PyStringObject {
value = "Hello"
}
python
>>> x = "Hello"
>>> x = x.lower()
Python for Data Science
x PyStringObject {
value = "Hello"
}
PyStringObject {
value = "hello"
}
python
>>> x = "Hello"
>>> x = x.lower()
Python for Data Science
x PyStringObject {
value = "Hello"
}
PyStringObject {
value = "hello"
}
python
>>> x = "Hello"
>>> x = x.lower()
Python for Data Science
x PyStringObject {
value = "Hello"
}
PyStringObject {
value = "hello"
}
python
>>> x = "Hello"
>>> x = x.lower()
Python for Data Science
>>> x
'hello'
x PyStringObject {
value = "Hello"
}
PyStringObject {
value = "hello"
}
Python for Data Science
>>> x = 3
>>> print(x,", ",y)
python
>>> x = 7
Python for Data Science
x PyIntObject {
value = 7
}
python
>>> x = 7
>>> y = x
Python for Data Science
x PyIntObject {
value = 7
}
y
python
>>> x = 7
>>> y = x
Python for Data Science
>>> x = 3
x PyIntObject {
value = 7
}
y PyIntObject {
value = 3
}
python
>>> x = 7
>>> y = x
Python for Data Science
>>> x = 3
>>> print(x,", ",y)
x PyIntObject {
value = 7
}
y PyIntObject {
value = 3
}
python
>>> x = 7
>>> y = x
Python for Data Science
>>> x = 3
>>> print(x,", ",y)
3, 7
x PyIntObject {
value = 7
}
y PyIntObject {
value = 3
}
Python for Data Science
Loops in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
int i = 0;
for(i=0; i < 10; i++) {
printf("%d\n",i);
}
}
python
for i in range(0,10):
Python uses
print(i) indentation
rather than
brackets.
C
#include "stdio.h" range(start, stop[, step])
int main() { Returns values between start
Python for Data Science
python
for i in range(0,10):
print(i)
python
for i in range(0,10,2): range(start, stop[, step])
print(i) Returns values between start
Python for Data Science
print(i)
i+=3
2
5
8
11
Python for Data Science
print(i)
i+=1
print(i)
i+=1
0 < 10 True
python
i = 0
while i < 10:
Python for Data Science
print(i)
i+=1
0 < 10 True
1 < 10 True
python
i = 0
while i < 10:
Python for Data Science
print(i)
i+=1
0 < 10 True
1 < 10 True
2 < 10 True
3 < 10 True
4 < 10 True
5 < 10 True
python
i = 0
while i < 10:
Python for Data Science
print(i)
i+=1
print(i)
i+=1
Conditions in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
print(i) from x / y.
For example, 22%3 is 1 because
0 22 / 3 is 21 R1
2
What should I put here to
4 print just the even values?
6
8
python
for i in range(0,10): % (modulo)
if i % 2 == 0: x % y produces the remainder
Python for Data Science
print(i) from x / y.
For example, 22%3 is 1 because
0 22 / 3 is 21 R1
2
What should I put here to
4 print just the even values?
6
8
python
for i in range(0,10): % (modulo)
if i % 2 == 0: x % y produces the remainder
Python for Data Science
print(i) from x / y.
For example, 22%3 is 1 because
0 22 / 3 is 21 R1
2
4
6
8
python
for i in range(0,5): % (modulo)
if i % 2 == 0: x % y produces the remainder
Python for Data Science
print(i) from x / y.
# fill in missing For example, 22%3 is 1 because
22 / 3 is 21 R1
# code
0
What do I need to change
11 to print out the values on
2 the left (hint, for odds, it is
13 print 10+i)?
4
python
for i in range(0,5): % (modulo)
if i % 2 == 0: x % y produces the remainder
Python for Data Science
print(i) from x / y.
else: For example, 22%3 is 1 because
22 / 3 is 21 R1
print(i+10)
0
11
2
13
4
python
for i in range(0,5): % (modulo)
if i % 3 == 0: x % y produces the remainder
Python for Data Science
print(i) from x / y.
elif i % 3 == 1: For example, 22%3 is 1 because
22 / 3 is 21 R1
print(i+10)
else:
print(i-10)
0
11
-8
3
14
Python for Data Science
Functions in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
return val
print(my_abs(-7))
7
python
def my_abs(val):
if val < 0:
return 0-val
Python for Data Science
return val
print(my_abs(“Hi”))
else:
print(val)
x = print_abs(-2.7)
print(x)
else:
print(val)
x = print_abs(-2.7)
print(x)
2.7
None
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
x = 7
inc_val(x)
print(x)
7
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
val
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
val PyIntObject {
value = 7
}
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
val PyIntObject {
value = 7
}
python
def inc_val(val):
val = val+1
Python for Data Science
x = 7
inc_val(x)
print(x)
7
x PyIntObject {
value = 7
}
Python for Data Science
x = 6
y = 3
swap(x, y)
print(x,”, “,y)
What is printed?
A. 6, 3
B. 3, 6
C. 3, 3
D. 6, 6
def swap(val1, val2):
tmp = val1
val1 = val2
val2 = tmp
Python for Data Science
x = 6
y = 3
swap(x, y)
print(x,”, “,y)
What is printed?
A. 6, 3
B. 3, 6
C. 3, 3
D. 6, 6
Python for Data Science
Scope in Python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
return val
print(val)
python
def my_abs(val):
if val < 0:
return 0-val
Python for Data Science
return val
print(val)
return 0-val
return val
practice
print(val)
python
my_val = 0
def my_abs(val):
if val < 0:
Python for Data Science
return 0-val
return val
"my" denotes global
print(my_val)