Easy Intro to Python
Although I’ve worked in the data space for the past 5 years, my responsibilities have not required me to learn to code. I mostly focus on ensuring the data is structured properly and visualized effectively.
Coding/ programming has always sounded exciting; therefore, I decided to learn more about it. I took a few courses on R but have decided to move forward with becoming proficient in Python.
The first step was to install Python (link to install python). I watched a brief video on how to test if the installation worked properly (link to video). I typed my first “Hello World” in Python — so exciting 🙂
I then typed the same code in the IDLE Python app; and it worked!
My next step was to install Geany; a simple text editor, which makes it easy to run Python programs. Output is displayed in a separate terminal window, which gets you used to working in terminals as well.
The next step was to learn more about storing information in variables. The two types of data I started with are 1) strings, which are sets of characters, and 2) numerical data types.
A variable holds a value. You can change the value of a variable at any point. For example, we can create a variable called ‘message’ and have that equal to some sentence, for example Message = “Data Rocks”. Then when you go to Print (Message) and run the code, you’ll get “Data Rocks” as the output.
- Variables can only contain letters, numbers, and underscores. Variable names can start with a letter or an underscore, but can not start with a number.
- Spaces are not allowed in variable names, so we use underscores instead of spaces. For example, use message_type instead of “message type”.
- You cannot use Python keywords as variable names. See table below for keywords.
- Variable names should be descriptive, without being too long.
Strings are sets of characters. Few nuances about cases in strings -> it is often good to store data in lower case, and then change the case as you want to for presentation. This catches some typos. It also makes sure that ‘mary’, ‘Mary’, and ‘MARY’ are not considered three different people. Here are three case change examples; if we saved first_name = mary:
print(first_name.title()) = Mary
print(first_name.upper()) = MARY
print(first_name.lower()) = mary
A variable name can be followed by a dot and then the name of an action, followed by a set of parentheses:
For example, the ‘title’, ‘upper’, ‘lower’ are actions that have been written into the Python language). The parentheses may be empty, or they may contain some values.
It is often very useful to be able to combine strings (concatenate) into a message that we want to display. For example, if we take the first name Kate and last name Strachnyi, fields we can add them together to create a full name. See below image for example of how the code looks before we execute:
The output = Kate Strachnyi
Dealing with numbers… basic integer operations work just as you may expect them to. Below are the sample symbols used for these operations:
Comments allow you to write in notes, within your program. In Python, any line that starts with a hash or pound (#) symbol is ignored by the Python interpreter. Writing good comments is one of the clear signs of a good programmer. If you have any real interest in taking programming seriously, start using comments now.
So, what makes a comment good?
- It is short and to the point, but a complete thought.
- It explains your thinking, so that when you return to the code later you will understand what you were trying to do
- It explains your thinking, so that others who work with your code will understand your overall approach to a problem.
- It explains particularly difficult sections of code in detail.
Learning Python seems like a super fun journey and I’m excited to dive deeper!