This tutorial is aimed for those who have little to no programming knowledge, so I will cover the basic programming principles here.
I will use a unfold approach, so in order to make it easier to understand, I will sometimes give approximated definitions for each concept and try my best to bring the precise definitions until the end of the course.
Programming is a way of telling computers to do stuff. This can be sending emails, doing calculations or displaying fancy 3d game characters on the screen.
You can tell computers what to do in multiple ways, from schedule emails on Gmail, setting up your phone's alarm clock or using programming languages.
Programming languages are more flexible, but complex ways to tell computers to do stuff. One popular programming language is Python.
Python scripts are just a sequences commands of what you want the computer to do.
The most simple command you can write is print(...)
. It only displays messages on the screen
Open Abstra Cloud, start a new form and type this:
print("Hello there!")
# Lines starting with # do nothing, they are just comments
print("Welcome to Python tutorial")
# print("this should do nothing")
print("But this should")
Pretty boring right?
Using Abstra, you can display text in a much more friendly manner. Try this:
from hackerforms import * # Ignore this line for now
display("Now this is getting somewhere")
display("But I'm still bored")
Computers can do much more than just displaying text.
# Still ignore this line
from hackerforms import *
# Math?? really? I just want solve my problems, not create more
display(1 + 2 + 3)
Just like in math, you can assign variables here. Let's say x = 3 + 4
. What is the value of x
?
from hackerforms import *
x = 3 + 4
display(x)
In this case, x
is a variable, but different from math, variables here can change over time:
from hackerforms import *
x = 1 + 2
display(x)
x = 5
display(x)
Also, you can name your variables more creatively than just x
and use multiple variables:
hello = 1
darkness = 1 + 2
my = 5/3
old = hello + darkness
friend = my * my
display(hello)
display(darkness)
display(my)
display(old)
display(friend)
Extra:
- What kind of names can you use with variables?
- Can you explain why
5/3
is2
and not2.5
?
Variables can also be other things than numbers
For instance: texts (also known as strings
)
from hackerforms import *
first = "abstra"
last = "cloud"
display(first + last)
Extra:
- Can you explain the difference betweeen strings and numbers?
- Can you display a text with multiple lines?
- Can you display a text with
"
inside?
Still bored? Please, stay here and I promise it will get better 🙏
Abstra allows you to ask info from the user, so they can interact with your script doing more than just pusing buttons.
from hackerforms import *
name = read("What is your name?")
display("Hello " + name)
You have all kind of inputs for asking questions
from hackerforms import *
read_number("How old are you?")
read_image("Upload your profile picture")
read_currency("How much would you like to donate?")
If you are curious, see all widgets we have in the docs.
Extra:
- Can you build a calculator for summing up numbers?
- Can you build a build a form?
- What happens when you try to combine numbers and strings?
Programs are not just a sequence of pre-defined steps. You can change what happens depending on user input.
from hackerforms import *
age = read_number("How old are you?")
if age < 18:
display("You shall not pass!")
# code here just runs when the condition `age < 18` is satisfied
else:
display("Welcome!")
# code here run otherwhise
# everything after the `if` command will happens
display("This is happens everytime")
Here is another example
from hackerforms import *
display("Knock, knock")
the_one_who_nocks = read("Who's there?")
display()
This is a little bit tricky, so try to play around a little bit with the code to see if you fully understoo this if else thing.
Extra:
- What happens if you remove these weird spaces at the beginning of the line
- What happens if you remove the
else
part - What happens if you add another
if
inside the block - Can the condition
age < 18
itself to be assigned to a variable? What kind of variable are this? - Can you build a BMI calculator?
Ok. here you passed the first inflection point, so you are able to build small calculations programs.
We will start to dive a little bit deeper
Sometimes you want to reutilize some logic just like you do with variable values. This is useful for make your code smaller and also easier to read.
from hackerforms import *
def sum(a,b):
return a+b
display(sum(1,1))
display(sum(1,2))
display(sum(2,3))
display(sum(3,4))
This weird code with def ...
is what we call functions. Functions as you may not remember from your math classes is a map between an input and an output.
Every function is defined (thus def
) in the same way:
def name_of_the_function(function,parameters,come,here):
# this is the function body
return the_result_of_function
When you need to call this function, you can use just as other commands you saw:
name_of_the_function(arguments,to,the,function)
This is just the way we say in Python that we are defining a function. def
s are followed by ...
This is a name you give to this function, so you can call it latter.
Parameters are placeholder variables that you can use to specify what your function should do.
Finally, as functions have outputs, return
statements are the way you tell what should be the result of this function.
from hackerforms import *
def factorial(n):
if n <= 1: # `<=` means smaller than or equal to
# factorial(0) and factorial(1) is 1
return 1
else:
# this function call itself, this is called recursion
return n * factorial(n - 1)
display(factorial(6))
Extra:
- Can you build a
double
function that returns the double of a given number? - Can you build a
multiply
function for 2 different numbers? - What happens if you try
factorial("abstra")
? - What happens when you remove
return
from the function? - Are
read
,display
,print
, etc.. functions?
Sometimes you want work with tables or sequences of information
from hackerforms import *
email = read_email("Enter your email")
authorized_emails = [
"[email protected]",
"[email protected]",
"[email protected]"
]
# `in` checks if left item is inside right list
if email in authorized_emails:
display("Welcome " + emails)
else:
display("Access denied")
Lists are those things between [
squared brackets ]
. They are just sequences of values organized in a single object.
You can use lists for many things, for instance:
from hackerforms import *
choice = read_multiple_choice("Which plan do you want to sign?", [
"Free",
"Starter",
"Standard",
"Professional"
])
Each item inside a list can be accessed by its position. But here is the catch: positions stars from 0, not from 1.
from hackerforms import *
todo_list = [
"learn python",
"use abstra",
"be a the hero of my company"
]
display(todo_list[0]) # 1st element
display(todo_list[1]) # 2nd element
display(todo_list[2]) # 3rd element
Extra:
- What happens if you try to access an position where there is no element?
- What happens if you try to access negative positions?
- Can you mix numbers and strings inside a list?
Now you know lists, you can tell computers to do repetitive work using for
s
from hackerforms import *
employees = [
"Michael",
"Dwight",
"Jim",
"Pam",
"Erin",
"Andy",
"Kevin",
"Toby",
"Angela",
"Kelly",
"Ryan",
"Stanley",
"Meredith",
"Oscar",
"Phyllis",
"Creed"
]
for employee in employees:
read_multiple_choice("how would you rate " + employee + "'s performance?", [
"Awful",
"Meh",
"Yep.. good",
"My favorite!"
])
All for statements have the same structure:
for item_name in list_name:
# Do stuff here
# This inner part of the for is called "body"
This is just a keyword that tells python to start a for loop.
This is a placeholder name for a variable that will assume the value of each item in the list for its respective iterations
This is just a notation for separating the item in the list from the list itself
Here you can add a list variable or its value directly
This is the code that will be executed multiple times. One time for each item in the list
Extra
- Can you make a function
length(list)
that returns how many items this list have? - Can you make a function
maximum_value(list)
that receives a list of numbers and returns the maximum value? and the minimum? - What about a function
get_sum(list)
that sums all numbers in the list? - What happens if you sum 2 lists?
list1 + list2
- Can you build a program where people enters income and expenses in the company and you print out the balance?
You don't have to always build your functions from scratch, you can (ans should) always use functions that are already written for your.
Here is a list of functions you can use to work with lists: link.
The same apply for strings (link) and more.
You can use Python to read and write files. Here is the simplest way:
file_content = open("filename.txt", "r").read()
This will open an file with name filename.txt
and assign the variable file_content
with its content.
This file must exist or an error will be throwed. You can add files in the files tab on your workspace. Also is important that this method will only work with text files or files that can be opened in a text editor (such as .csv or .json)
open("filename.txt", "w").write("file content here")
As you may guess, this writes in the file. It is important to know that this will replace any existing content in this file. If the file don't exist, this will create a new one.
open("filename.txt", "a").write("file additional content here")
This adds content in the end of an existing file. If this file doesnt exists, this will create it.
Abstra allows you to get files from user using read_file.
from hackerforms import *
uploaded_file = read_file("submit a txt with all new emails you want to register separated by lines")
lines = uploaded_file.content.split("\n")
for line in lines:
display(line)
split transform a string into a list by spliting it, therefore "foo bar baz".split(" ")
is ["foo", "bar", "baz"]
Extra:
- Can you write a program that reads a txt file from the user and prints our the total number of words?
- Can you write a program that reads a csv file from the user with orders (columns: client_name, product_name, unit_price, units) and prints out the total spent for each customer?