jpj251@nyu.edu
¶int
)float
)str
)bool
)list
)foo = -7
print(foo)
-7
foo
-7
new_foo = 10
new_foo
10
bar = 5.5
bar
5.5
check_var = 5.0
check_var
5.0
bar_alt = 5
bar_alt + bar
10.5
string_var = 'This is my string'
print(string_var)
This is my string
string_var
'This is my string'
print("hello world")
hello world
bar_string = '-7'
foo_string = '5.0'
bar_string
'-7'
foo_string
'5.0'
bar_string + foo_string
'-75.0'
bar_string*5
'-7-7-7-7-7'
bar
5.5
bar_string*bar
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-27-d46a3ac220f1> in <module> ----> 1 bar_string*bar TypeError: can't multiply sequence by non-int of type 'float'
foo
-7
foo_string
'5.0'
foo + foo_string
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-30-9857000bcc8c> in <module> ----> 1 foo + foo_string TypeError: unsupported operand type(s) for +: 'int' and 'str'
foo_string
'5.0'
bar_string
'-7'
foo_string + bar_string
'5.0-7'
bool_var = False
bool_var
False
var1 = 10
var2 = 20
var1 == var2
False
bool
) Continued¶var1 != var2
True
var1 < var2
True
var1 <= var2
True
var1 >= var2
False
new_var = "True"
new_var
'True'
my_name = "Jeff"
my_other_name = "Jeff"
my_name == my_other_name
True
my_full_name = "Jeffrey"
my_name == my_full_name
False
my_name in my_full_name
True
foo
-7
type()
function¶type(foo)
int
foo_string
'5.0'
type(foo_string)
str
foo_type = type(foo)
foo_type
int
type(foo_type)
type
foo_string
'5.0'
float(foo_string)
5.0
int(foo_string)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-56-979cc2db2ab9> in <module> ----> 1 int(foo_string) ValueError: invalid literal for int() with base 10: '5.0'
int("1254")
1254
my_int_str = '5'
int(my_int_str)
5
int(5.0)
5
foo_string
'5.0'
float(foo_string)
5.0
int(float(foo_string))
5
float(foo_string)
5.0
int(float(foo_string))
5
new_string = "not a number"
float(new_string)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-66-6de1d62688ec> in <module> ----> 1 float(new_string) ValueError: could not convert string to float: 'not a number'
new_float = 5.999999999
int(new_float)
5
Let's create a list
my_list = []
my_list
[]
my_list2 = [1,23]
my_list2
[1, 23]
mix_list = [8,5.0,'5.0']
mix_list
[8, 5.0, '5.0']
mix_list
[8, 5.0, '5.0']
mix_list[1]
5.0
mix_list[0]
8
mix_list[2]
'5.0'
mix_list[5]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-79-efefc08ff73a> in <module> ----> 1 mix_list[5] IndexError: list index out of range
mix_list[-2]
5.0
elem_2 = mix_list[2]
elem_2
'5.0'
mix_list[2] ="A different string"
mix_list
[8, 5.0, 'A different string']
len(mix_list)
3
number_list = [423,34,1]
sum(number_list)
458
min(number_list)
1
max(number_list)
423
sum(number_list)/len(number_list)
152.66666666666666
number_list = [423,34,1, 423]
number_list
[423, 34, 1, 423]
number_list.remove(423)
number_list
[34, 1, 423]
del number_list[1]
number_list
[34, 423]
number_list
[34, 423]
number_list.append(27)
number_list
[34, 423, 27]
import numpy as np
my_arr = np.array([1,2,3])
my_arr
array([1, 2, 3])
my_arr*5
array([ 5, 10, 15])
my_arr**3
array([ 1, 8, 27], dtype=int32)
num_list = [1,2,3]
num_list*5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
my_list**5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-107-60150ab04e48> in <module> ----> 1 my_list**5 TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
np.mean(my_arr)
2.0