Programming Datatypes#

There are a variety of ways to store data inside of a program. Some data types are more effective than others in certain tasks, so selecting your datatype is imporant.

Below in the general code, you will see how each type of value is stored.

# Basic data types:

var1 = 3 #Integer (whole numbers)
print(type(var1),",", var1)

var2 = 3.14159 #Floats (numbers with decimals)
print(type(var2), ",", var2)

var3 = "Hello there!" #Strings (Things other than numbers (but can include Numbers))
print(type(var3), ",", var3)

var4 = "My favorite number is 3, because it is a good approximation for \u03C0, e, and sometimes 4." #string with numbers
print(type(var4), var4)

var5 = True #boolean
print(type(var5), var5)
<class 'int'> , 3
<class 'float'> , 3.14159
<class 'str'> , Hello there!
<class 'str'> My favorite number is 3, because it is a good approximation for π, e, and sometimes 4.
<class 'bool'> True

Combining Datatypes#

Depending on the type of operation you perform on different datatypes will sometimes result in different datatypes, so keep that in mind as you add and subtract and so on. Some of the combinations give errors, those are commented out for the main setup of this page. Feel free to uncomment when tinkering.

#test Values
int1 = 5
int2 = 6

float1 = 1.5
float2 = 2.5

string1 = "one"
string2 = "two"

print(int1 + int2) #int + int = int

print(int1 * int2) #int * int = int

print(int1 / int2) #int / int = float

print(int1 // int2) #int // int = int

print("")

print(int1 + float1) #int + float = float

print(int1 * float1) #int * float = float

print(int1 / float1) #int / float = float

print(int1 // float1) #int // float = int (will almost always be printed as x.0 though)

print(float1 // int1) #float // int = int (will almost always be printed as x.0 though)


print("")

print(float1 + float2) #float + float = float

print(float1 * float2) #float * float = float

print(float1 // float2) #float // float = int (will almost always be printed as x.0 though)

print("")

print(string1 + string2) #str + str = str

#print(string1 * string2) #str * str = type error

print(string1 * int1) #str * int = str

#print(string1 * float1) #str * float = type error

#print(string1 / int1) #str / int = type error
11
30
0.8333333333333334
0

6.5
7.5
3.3333333333333335
3.0
0.0

4.0
3.75
0.0

onetwo
oneoneoneoneone

Sequence Datatypes#

Sometimes data is more effective when we have a lot of it instead of just one number or string. For this, we can use sequence datatypes to store multiple pieces of data in a single variable. Below are some examples, feel free to tinker.

#Lists are the most universal datatype in programming.  Often also called arrays (but be careful, arrays are a type used in the numpy libary, and act quite differently)
#represented with square brackets
#indexing starts at 0
list1 = [1,2,3,4,5,5,4,3,2,1]
print(list1)

#you can get a variety of information about the list too.  Length (len()) tends to be very helpful
print(len(list1))

#Lists are ordered, once you place a value in a certain location, you can always find it in that location
#Find different items in the list by using x[i] where x is your variable and i is your location. Note that we start at 0!
print(list1[0])
print(list1[2])
print(list1[4])

#Adjust values in a list by either reassigning a location, or we can adjust values using our normal operators
list1[1] = 23
print(list1[1])

list1[3] = list1[3]*2
print(list1[3])

# You can take a chunk of the list by using [start:stop] as values inside the location area
#This is called slicing
print(list1[:3])#get the First 3 values
print(list1[3:])#get everything after the first 3 values
print(list1[-3:]) #get the last 3 values
print(list1[3:6]) #get 3 middle values

#If we want to add values to a list, we need to be careful about doing so.  Do not try to just add directly to the end of a list, you will get an error
#list1[11] = 31

#use the append() function to add things to the end of the list
list1.append(123)
print(list1)

#using the remove() function will remove the first instance of the value provided (be careful with this, but it can be helpful)
list1.remove(3)
print(list1)

#Lists can include different datatypes too! just becareful, if you try to do math operations to things that aren't numbers, you may run into errors!
list2 = [1,2,3,"a","b","c",True, False]
print(list2)
[1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
10
1
3
5
23
8
[1, 23, 3]
[8, 5, 5, 4, 3, 2, 1]
[3, 2, 1]
[8, 5, 5]
[1, 23, 3, 8, 5, 5, 4, 3, 2, 1, 123]
[1, 23, 8, 5, 5, 4, 3, 2, 1, 123]
[1, 2, 3, 'a', 'b', 'c', True, False]
# Strings are basically just lists that are characters instead of numbers. Formatting is a little different in places, but not too bad.

str1 = "hello There"
print(str1)

print(str1[3])

#Instead of neeing to use the append() function, just add stuff to the end using the + operation.
str1 += "!"
print(str1)
hello There
l
hello There!
#Sets are similar to lists, but they can only include unique values

set1 = {1,2,3,4,5,5,4,3,2,1}
print(set1)

#add and remove things using the same functions as lists, but remember that if the value already exists, it won't appear in the set
set1.add(-3)
print(set1)

#Sets are unordered, meaning the values can be in any order.  Using an address does not work in sets
#print(set1[2])
#Instead, use "in" to determine if something is in the set or not
print(3 in set1)
print(7 in set1)
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, -3}
True
False
#Tuples are ordered, unmodifiable lists, meaning once you define them, you cannot change them. 

#tuples are defined using parentheses

tup1 = (1,2,3,4,5,6)
print(tup1)

#You can access the values inside of a tuple the same way you access a value in a list using x[i].  Remember indexing starts at 0!

print(tup1[3])

#You cannot add/append/remove tuples
#tup1.append(3)

#You cannot modify values in tuples
#tup1[3] = 9

#You cannot change locations of values in tuples
#tup1[3] = tup1[5]

#Tuples are helpful if you want to store data, and make sure it doesn't accidentaly get changed in a different part of your program
(1, 2, 3, 4, 5, 6)
4
#Dictionaries allow us to store data and find it based on a key.  
#This is extremely helpful for making classes of variables, such as if we wanted to store information on an RPG character

dict1 = {"name": "Mr. FumbleFingers", "class":"Artificer", "race":"goblin", "level":14, "items":["wrench","crossbow",{"bolts":3}]}
print(dict1)

#Dictionaries work by pairing a key with a value.  You can access information by searching for a key
print(dict1["name"])
print(dict1["items"])
print(dict1["items"][2]) #If you are finding a list/tuple in a value, you can access the different locations in it like ususal
print(dict1["items"][2]['bolts']) #Want a dictionary in your list in your dictionary?

#You can add things in dictionaries by assigning a new key to a value
dict1["mount"] = "Pig"
print(dict1)

#You can also add multpile things by using the update() function
dict1.update({"weakness":"Can't go anywhere without his pet rock","trait":"Likes to cook, but no one likes his cooking"})
print(dict1)

#you can remove things using the del keyword (a keyword is different than a function, and formatting is a little different, be careful)
del dict1["mount"] # :(
print(dict1)
{'name': 'Mr. FumbleFingers', 'class': 'Artificer', 'race': 'goblin', 'level': 14, 'items': ['wrench', 'crossbow', {'bolts': 3}]}
Mr. FumbleFingers
['wrench', 'crossbow', {'bolts': 3}]
{'bolts': 3}
3
{'name': 'Mr. FumbleFingers', 'class': 'Artificer', 'race': 'goblin', 'level': 14, 'items': ['wrench', 'crossbow', {'bolts': 3}], 'mount': 'Pig'}
{'name': 'Mr. FumbleFingers', 'class': 'Artificer', 'race': 'goblin', 'level': 14, 'items': ['wrench', 'crossbow', {'bolts': 3}], 'mount': 'Pig', 'weakness': "Can't go anywhere without his pet rock", 'trait': 'Likes to cook, but no one likes his cooking'}
{'name': 'Mr. FumbleFingers', 'class': 'Artificer', 'race': 'goblin', 'level': 14, 'items': ['wrench', 'crossbow', {'bolts': 3}], 'weakness': "Can't go anywhere without his pet rock", 'trait': 'Likes to cook, but no one likes his cooking'}

Casting/ Changing Datatypes#

Sometimes you want a float when you have an int, and sometimes you have string when you want to add it like a number. In limited circustances, you can change your datatype by ‘casting’ your data as a different type. It doesn’t always work, but if you need your data to look like a different datatype, it is worth trying.

str1 = "332"
print(str1)

#print(str1 + 1) #This will give you a type error

intOfStr1 = int(str1) + 1 #This will work
print(intOfStr1)

#different things you can cast:
#int() to convert to integers
#float() to convert to  floats
#str() to convert to  strings
#bool() to convert to booleans
#list() to convert to a list (needs to be iterable, like a tuple)
#tuple() to convert to a tuple
#set() to convert to a set
#dict() to convert to dictionaries (almost never works properly fyi)
332
333
#Sandbox Area