Programming, at least procedurally, is made up of telling the computer what you want the computer to do and when you want the computer to do it. This is the logic of a program. If you understand how to use the 7 main logical concepts then you can code in any language that you want. When you go to a new language you have a headstart and can work concentrate on understanding the syntax of that language more. Syntax can easily be looked up for the language of your choice. So when learning to program focus first on mastering the concept or logic of programming in general, the computer will tell you when you make a syntax error (see Programmers Mindset Help for Beginners for some additional insight). Below are the 7 concepts along with a brief description.
- Comments - creating text in a program that is not executed. This allows you to document your code so you and others will know what you are trying to accomplish.
- Data Types - allows you to handle text, numbers, and dates differently. Just think about sorting. Numbers, dates and text sort differently. Text of the following characters sort like: “1”, “11”, ‘111”, ”2”, “3”, “4”, “41”, “411”, “42” if they were numbers then the sort would be 1, 2, 3, 4, 11, 41, 42, 111, 411. Put “ around the characters in many languages to make it them are character types.
- Variables - allow you to assign a placeholder for data that you want to access in the future.
- Input - allows you to get information into the program.
- Output - allows you to send data to screen, printer, files or other external devices.
- Functions - allows you to organize and group commands together in a logical building block way.
- Internal - These are commands that are built into the programming language.
- User created - These are command combinations that you put together to be able to accomplish a task multiple times with possibly slight variation. The variations are accomplished by passing in parameters.
- Controls - allows you to control the flow of the execution of a program.
- Branching - allow you to make decisions based on data as to the path to take through your code.
- Loops - allows you to do a similar activity multiple times without code duplication.
Below is an example of each of these concepts using Python as an example language but keep in mind that the logic is what we are demonstrating. The same logic can be done in another language with just different syntax (see “Programming Mindset for Beginners”) for discussion on syntax and logic.
# This is a comment
All languages allow you to add comments to your code. This is how you can do it in Python so that the text after the # does not get executed.
2. Data Types
# this is a number
# this is a data type
Above is an example of different data types.
a = 5 b = 6 c = a + b Print (c)
All languages have the ability to store values in a temporary location. Above is how you represent variables in Python.
Input - get information from the keyboard
age = input(“What is your age?”)
Input allows you to get information into your program from the outside world. The input variable in Python is an example of a program call that will wait for a keyboard input before continuing the execution of that program.
Read_csv - get information from a file
import pandas as pd pd.read_csv(“datafile.csv”)
Input can also come from external sources. The program above gets input from a data file.
Print - print out information to the screen
print(“This is your age”.format(age))
All programming languages have a way of printing out information to the screen. This is how you would print out information in Python.
Write DataFrame - write information to a file
Import pandas as pd df.pd.to_csv(‘outputfile.csv”)
Languages are able to write information back to a file. In this case, Python will write the information in a DataFrame to a CSV file. Don’t worry if you don’t understand DataFrame or CSV. Those are terms you will learn if you take a Python data science programming class.
Writing To A File
#this will create new file naming newfile (text/plain) # "w" is used for writing f = open('newfile', 'w') f.write('This is a test data') #write is used for writing f.closed #file is closed now
For more traditional output you can write to a file instead of to the screen. Here is an example in Python how to write to a file.
from random import randint randint(5, 10)
A function can be built in or imported from other libraries. Here is an example of how this can work in Python.
def print3randint(start, end): print(randint(start, end)) print(randint(start, end)) print(randint(start, end)) print3randint(0, 10)
Functions can also be user defined. Here is a user defined function in Python that has two inputs and prints out 3 random numbers between 0, 10 as defined by the parameters to the function.
x = 5. if x < 0: print('Number is less than zero') elif x == 0: print ('Number is zero') else: print('Number is greater than zero')
If statements allow you to make decisions in a program. This gives programs their versatility. The above is a code in Python that allows the program to make a decision about the value of x. In the above case, x is a 'Number is greater than zero'.
# for loop for i in range(5): print(i) #will start printing from zero(default) to range. # while loop number = 0 while number < 9: print ('The number is:', number) # after each loop number will be increment by 1 e.g 0=0+1 # will be 1 so now number will become 1. number = number + 1
Loops allow you to execute a set of programming steps repeatedly. The above Python examples are of a for loop (execute code a set amount of times ) and a while loop (to execute code while a condition is true)
The above description gives you a general understanding of programming logic. With this logic, you will be able to understand programming and not just a programming language. Now pick a language and work on the logic and the syntax with an understanding of the difference between each.
What are your thoughts about the best way to learn to program?