When you start to code yourself, with friends and on a team, you begin to hear different syntax and terminology. You may frequently hear:
"Where's your script?"
"Can you show me your algorithm?"
"Send me your Python"
These terms will become more familiar to you over time. Let's start with the basics.
What is a program?
A program is instructions on how to perform a computation. The computation could be a mathematical operation such as the sum of two numbers or the multiplication of two numbers. It could also be a more complex program such as start streaming a video upon pressing play or dispensing a beverage from a vending machine with the click of a button.
Programs look different in different programming languages, but here are some basic terms in about every language:
input: get data to later use in a program
output: display results - typically on a screen
math: mathematical operations such as addition and subtraction
conditional execution: check if a condition exists and run code to meet the condition
repetition: perform a repeated action
Altogether, a program is a sequence of instructions that typically takes input, performs some compution, likely with math, conditional execution and repetition to display output to the end user.
Writing Your First Python
Let's cover the defintions behind basic code snippets.
Variable Naming Best Practices
Assignment statements create a new variable (left-hand side) and give it a name (right-hand side).
pi = 3.14 my_name = "Dan"
Choose variable names that are meaningful. It's conventional to use lowercase letters and a single underscore between words for better readability.
price_of_a_cup_of_coffee = 2
However, I prefer fewer words and a name that's still understandable.
price_cup_coffee = 2
A statement is code that has an effect such as creating a variable or displaying values.
# the print statement below displays the output inside the parentheses print("my name is Dan") price_cup_coffee_san_francisco = 3
my name is Dan
When you type in a statement, you run your code to see output. When you run your code, the Python interpreter executes it - meaning it processes any statements.
Python code in a file is called a script. Python scripts have the .py extension.
Usually, you utilize a script for multiple lines of code, anywhere from three to thousands of lines of code.
Jupyter Notebooks are in interactive mode. So if I type:
Above, you see the output.
However, if you put "hello world" as a standalone line in a Python script and run it, you won't see any output. In the script, you need to write
print("hello world") to display output.
Programs often get long and complicated. To help you and others understand your code, you can utilize comments.
Comments are natural language explanations of your code and are not executed by the Python interpreter. Single-line comments begin with a hashtag followed by text.
# price_cup_coffee in U.S. dollars price_cup_coffee = 2
You can alternatively put comments after a code statement.
price_cup_coffee = 2 # U.S. dollars
Order of Operations
Remember in grade school when you learned about PEMDAS - parentheses, exponents, multiplication, division, addition and subtraction.
These same rules apply with programming in Python.
The expression first evaluates the operation inside the parentheses, then the division operation.
An algorithm is a process for solving a problem.
A simple algorithm could be to calculate the absolute value of any integer.
If a number is already positive, we perform no computation. However, if a number is negative, we want the positive equivalent. So, we multiply the negative number by -1 to get the absolute value of our original number.
number = -5 if number >= 0: pass else: number = number * -1 print(number)
Where to Start Your Programming
So are you inspired and ready to get started learning programming?
Well, this site might not be the place to initially get started. I built this site to document my learnings and improve my own skills.
I'd recommend you start with linear-path tutorials and use this site later as a reference for cool tips and tricks.
If you want to get started in Python and eventually learn data science, I'd recommend these resources:
There's no shortage of free and amazing resources out there! These are just a few - but utilize any recommendations from your friends or your own research. The best thing about learning online is that if you don't like a resource, simply stop and move to another one! There's no one defined textbook you must read from cover to cover.
If you have any questions on your journey, please reach out: email@example.com