Learning Programming Fundamentals using Python
By: Kristian Roopnarine
Tags: Python, Coding, Developer, Computer Science, Programming

Background

I am a self taught developer that started learning to code in March of 2019. I just recently graduated with my M.S. in Chemistry and while job hunting I came across programming and didn’t look back. I’ve tried many different methods to learning to program and I’m here to share some of the ways that I think are most effective. Regardless of what you’re aspirations are, game developer, iOS developer, android developer, everyone needs to learn the basics of progamming.

The Fundamentals of Programming

I recommend that whatever language you pick, stick with it for a while, or at least until you have a firm grasp of programming concepts. Learn one thing at time, programming isn’t going anywhere.

What are these fundamentals?

Data types Variables Basic data structures (Lists/Dictionaries) Loops Conditionals Functions Classes and objects (more important in object oriented languages) Problem Solving

This list isn’t exhaustive, and this is coming from someone who created their own syllabus for learning. It is highly possible I’m leaving a concept out that might be relevant, the internet and documentation are your friend!

I’m pretty sure you’re wondering why Problem Solving is on there but that’s essentially what 99% of programming is, problem solving. I’ll show you some real examples of problem solving as we make our way through the fundamentals and methods to approach debugging an application.

Installing Python

We’re going to be working with the latest version of Python and to keep this article short and sweet, I’ll link a video to installing it below. We’ll be working with the Python shell for now so no need to worry about any text editor yet.

Installing Python: https://www.youtube.com/watch?v=h4fhdhNWDKk

Don’t worry about Visual Studio Code for now, just follow until the installation finishes.

Launch a Python Shell

Disclaimer — I am on windows, most of these commands will be windows specific. However a quick google search can quickly get the commands for a mac!

The great thing about programming is that almost anyone can get started. If you have a computer you can start coding in your command prompt (terminal for mac), and even on your phone you can download apps to code programs in Python. To launch a Python shell we need to open our command prompt, in Windows 10 you can do that by typing “cmd” in the bottom left search bar, and for the mac users you’ll look for the Terminal. Once it’s open you should see the window that makes you feel like a hacker.

Command Prompt in Windows 10

Launching a Python shell from here is very simple, just type in py (windows) and hit enter . Your command prompt should change slightly to:

Python Shell

From here we can type in any valid Python code! Let’s start by exploring data types and what they are.

Data Types

All programs essentially do one thing, they take in some data, manipulate that data, then output some data. How the program manipulates that data depends on that data’s type. The data types below are found in Python with many more, and may be found in some form in other languages.

Strings

Integers

Float

Boolean values (True or False)

Strings

Strings are a way to represent any sequence of text. The syntax for a string is wrapping the text in either a pair of double quotes , "" or single quotes '' . You cannot mix and match single and double quotes or Python will throw the following syntax error:

Syntax error when not properly closing the double quotes.

The proper way to write a string in Python is:

We can even write numbers inside of the double quotes:

String containing a number.

This is where the difference between data types because very important. Based on the data type Python will know how to handle certain operations. You’ll notice that the 2 is wrapped in quotes '2' , this tells Python that this is a string. If we try to add "2+2" something very weird happens.

When we try to add 2+2 inside of a string, it doesn’t return 4, it returns the string "2+2" . Why is that? Well it’s because Python isn’t evaluating the expression 2+2, because we wrapped the addition inside of the quotes Python is treating it as a string data type. We CAN add strings together which is called concatenation, but the result is not something you’d expect and we’ll cover that when we talk about variables.

However, when we write the expression 2+2, Python treats them as integers and knows to add them together, which introduces our next data type, Integers.

Integers and Floats

Python represents numerical data in two types, integers and floats. We can perform calculations using them with arithmetic operators because Python follows PEMDAS.

The integer data type is pretty simple, it is any positive or negative WHOLE number. If the number has any decimals, for instance 1.0 , Python with not treat them as an integer! Instead any number with decimals are part of the Float data type. The difference is subtle and more important when dealing with arithmetic operations. Python supports the typical arithmetic operations shown below:

Arithmetic operators

If you add, subtract, divide, or multiply an integer by a float, the result will ALWAYS be a float. This may not seem huge but if you’re performing any conditionals or checks that rely on an integer data type, your code may break!

Boolean data type

The last data type that I mentioned is a boolean, which can only be two values, True or False , the capitalization matters! These will become important when we talk about conditionals because we’ll use these boolean values to execute different blocks of code depending on the situation.

Is there a way to check for these different data types?

I’m glad you asked! Python provides us with an built-in function called type(argument) that will return the data type of the argument.

Built-in function type() returning one of every data type.

Wrapping up

I want to keep these articles as short as possible because I don’t want to flood anyone with information.

Recap:

Data can exist in different types in Python (and other languages) , and Python can perform specific operations depending on the type of data. The four types covered today were Strings, Integers, Floats and Booleans.

In the next part we’ll talk about Variables, which is a place to store data and we’ll see how we can manipulate these data types.

I encourage you to experiment with these data types, can you add a string and integer together? What about a string and a boolean? Does True + False evaluate to anything? Can you divide by 0? Is there infinity?

If you encounter any errors in the Python shell, try copying and pasting the error into google to understand why it’s happening.