Python, the Great Language for Data

Introduction to Python Programming by learning the Essentials

Richard Mei
4 min readDec 14, 2020

Nowadays, Python is everywhere, and there’s obviously a reason to that! Python has been an object oriented programming languages since its inception and is not just a scripting language! Python is especially powerful for in development in that there is no compiling after writing code. By this I mean Python goes from reading, evaluating and printing as opposed to writing, compiling, testing, and recompiling. Now that you’ve been convinced to use Python, time to learn from the basics.

Variables and Types

A variable in Python is a little different from other languages in that it doesn’t store values/objects, rather acts as a pointer. They can be considered more as labels so we don’t need to actually declare any types (int,char, etc) like in other languages. To assign variables, we simply use an equal sign.

Python contains data types like numbers, string, list, boolean, tuple, set, and dictionary. Numbers can be integers, floats, or complex. The numbers, strings, and tuples are immutable meaning they cannot have their values changed. For mutable objects like dictionaries, lists and sets, you can change the values stored. Some examples are below:

example_string = 'string'
example_string2 = "string"
example_int = 3example_boolean = Falseexample_tuple = ('object1','object2')example_list = [1,3,6,'apples']
example_dict = {'key1': 3, 'key2': 'oranges'}

You can make strings using either open and close apostrophes or quotation marks. Either case works and you should use quotation marks when your string has an apostrophe. If you don’t like, you could also use the escape character “ \ ”. Tuples are an unordered, and unchangeable group of objects. They are made with parenthesis and can be indexed. The boolean is either True or False and is used for logic. In Python, you can also use the integer 0 to represent False as well.

Next we have lists which are changeable in that values inside the list can be edited. We can also slice through them using something like example_list[1:3], which returns [3,6]. They start with an index of 0 and have other slicing rules you’ll come to remember through practice.

Dictionaries have keys and values, but not always both. The brackets and colons shown above is one of the ways to create dictionaries.

This site goes over a lot on these data types and is worth the time to look over. All of these data types are objects, all of which have a type, value, and identity. When we have the line example_int = 3, the type is an integer, the value is 3, and the identity is example_int.

Control Flow and Iterators

In Python, we have ways to add logic to our program, and always ways to iterate. First we have “if” statements that check the condition that follows, and if true, will execute everything in the “if” block. If we add more conditions to check, we can use an “elif” followed by another block. Our catchall remaining would be an “else”.

To use logic, we can simply say “and”/“or” or use symbols like “>”, “<”, “==” .

a = 1
if a > 2:
print("Greater Than 2")
elif a == 1:
print("Do you think this will run?")
else:
print("The others did not execute!")

Python reads this code block line-by-line, so the first condition would be false, reading the elif statement. Since a is 1, then it will run the block and print the question. If we want more operations to happen or run more methods/functions, we can keep adding to the block it belongs to.

Next we have a “while” loop that keeps on checking a condition and runs code if its true. Once it’s false, the loop will end. If we don’t have a condition that will eventually turn off our while loop, then it will keep on running until we manually break our program.

i = 0
while i < 10:
print(i)
i += 1

In the simple example above, outide the loop we have “i” to keep track of the iterations. Since it starts less than 10, it will continue to print “i” and then adding to i. When i reaches 11, the loop would stop and we would not print(i) nor would we add to i. In other languages we would use a line like “i++” to increment the “i”, but since we have “i” be the identity, incrementing a pointer wouldn’t make sense.

Finally we have “for” loops that will go through an entire iteratable object.

numbers = [1,2,3,4]
for number in numbers:
print(number)

Unlike the while loop, we don’t need any trackers outside the loop. We have a list of numbers that we want to go through and print. The “for” is followed by a variable/identity we want to temporarily give to the current iteration so we can manipulate or perform operations on it somehow. In this case I used the identity “number” .

Conclusion

Overall, Python is a great language and pretty easy to get started on. Hopefully this helped iron out some of the edges of your knowledge or taught you a whole lot about the syntax.

--

--