Learn to Code via Tutorials on Repl.it!

← Back to all posts
Absolute Beginner Head First into Python 3 - Not finished
Zeune

Lesson 1.00 - Common Terms


Module - A module in python is simply a file with the .py extension
class - is a code template for creating objects.
object - is simply a collection of data, variables and functions that act on the data.
function - are a set of statements that take inputs, do some specific action and may produce output. The idea is to put a commonly or repeatedly done task together and make a function to prevent writing the same code over again.
parameters - are the variables listed inside the parentheses in the function definition. Parameters are also commonly referred to as args or arguments.

variable (var) - A var is a reserved memory location to store different data types.
string -is a datatype representing unicode characters.
integer (int) - is a datatype that stores positive or negative whole numbers.
long - written like integers, but follows by an uppercase or lowercase 'L'. Long vars are of unlimited size
float - represent real numbers and are written with a decimal point. Floats may also be in scientific notation

Lesson 1.01 - Hello world!


Create a new repl.it project using the "+ new repl" button located in the upper right of the browser.

Then, proceeed to find and select Python in the drop down box and name your project something meaningful such as "Python Lessons" then click create repl. A new environment will appear with a files on your left, a coding environment (CE) in the middle and your console on the right.
In the CE type print("Hello world!")
The above line of code uses a default function called print which comes with the python library. The print function allows the user to output data that is passed in the parameters to the console window. You may notice the two quotations. Anytime you want to print text that is not an integer to the screen you will need to put between quotation marks. Most libraries / APIs have what are known as documentation associated with them, and can be read by hovering over the function. Perhaps you forget the function name print(), but you knew it started with a 'p', you could type the letter p and press ctrl+space to see the currently available functions. Functions with repl.it have purple hollow boxes. So when you press ctrl+space you should see two functions print() and pow()

A working program should look like the below image.

Lesson 1.02 - Modifying the Hello World w/ vars


This will hopefully be easy. Modify the Hello World program from Exercise 1.01 to say "Hello, " followed by your name. The output should look like

Hello Joshua

Once you successfully modified your program to say Hello followed by your name, we will begin to learn about concatenation and the use of variables.

At the moment all your code was probably happening on line 1. Because Python reads top down we need to define vars before we try to access them. To do this drop your current print code to line 3 and on line 1 we are going to create a variable that stores your name and call it in the function as a parameter.

Variable names in Python do not matter. You could name it anything you wanted, but for most people a name that describes the data being stored is preferred. Because we want to store the users first name, let us name our variable firstName. You may notice how the first letter is not capital, but the first letter of every word after is. This is typical naming conventions across many programming languages, as well as for function names. Now we use the = sign to assign the data to store, in this case the users first name. All strings must be written between quotes (single or double do not matter).
firstName = 'Joshua'

In our print statement delete the text "world" and then after the ending quotation mark use the '+' operator to concatenate the strings "Hello, " and firstName together. The final code should look similar to below.

Lesson 1.03 - Other classes functions


The data that we store in variables is represenative of python classes. In this case when we store our name in the variable firstName we are storing our data as a string. String (string) is another class within python, and the string class also offers different functions we can use to manipulate strings.

If you would please, change the capitalization to be random throughout your name. E.g "jOShuA"

Once you have adjusted the string add a new line below your current print statement. Below the print statement write

firstName = firstName.upper()

What the upper() function does is takes all characters in the string and turns them into capitals, and then rewrites the new all capital name as our new data in firstName. So when you write another print statement calling firstName it will display the new text is all capitals. Now, try and rewrite your name so that it displays in all lower case, and then again one more time print "Hello, " your name with only the first letter of your name capital. To make the first letter of a String capital you use the title() function. Code is below

Hello, jOShuA
Hello, JOSHUA
Hello, joshua
Hello, Joshua

Lesson 1.04 - Working with numbers and errors


Delete three of the four print statements, keeping the one with firstName.title(). Below your firstName var create a new variable named age and equal it to your current age. The code should look like age = 25. Then, concatenate your print statement so that your output is displayed like below

Hello, Joshua is 25 years old

You may notice if you try to write your code like print("Hello, " + firstName + age) disregarding the other content the console will throw an error like below

Traceback (most recent call last):
File "main.py", line 4, in <module>
print("Hello, " + firstName + age)
TypeError: Can only concatenate str (not "int") to str

To get a better understanding what the error is telling you. You will notice the Traceback tells you the line which threw the error and that the compiler is having problems with concatenation. The error is saying we can only concatenate strings, not integers. So, what we need to do is use a built in function called str() which will convert the data within to type str. rewriting your print function using str(age) should allow you to successfully run the program. Code and output below.

Hello, Joshua is 25 years old.

Some errors do not always crash the program. One example is forgetting to add a space before or after concatenating a string. This could lead to unwanted output. Such as if you mistakenly wrote the print statement as
print("Hello, " + firstName + "is " str(age) + " years old.")

Then, your output becomes

Hello, Joshuais 25 years old.

And it is obvious the above is not what we want. That is a simple error where we can adjust the print statement to have a space where see fit, but what about if you forget a quotation... what happens then?

The code below is broken. Run it, read the errors and try to fix. Don't worry we will review this together as well.

The above program has multiple errors not just one. When we run the program we start off with an error on line 1...

File "main.py", line 1
firstName = "Beta
SyntaxError: EOL while scanning string literal

syntax errors are one of the most common errors where it means something was not typed correctly. The error itself reads "End of line while scanning string literal." Which just says the compiler read to the end of the string literal and couldn't find where it stops. That is because the string Beta is missing the ending quotation. Once we add in the quotation and run the program again we come to our next error...

File "main.py", line 4
print("Hello, " + FirstName + "is " + str(age) + " years old.))
SyntaxError: EOL while scanning string literal

We get the same kind of error. Scanning a string and fails to find the end point. At the end of years old we do not close with a quotation. Let's add one and rerun the program.

Our next error is,

File "main.py", line 4
print("Hello, " + FirstName + "is " + str(age) + " years old.))
SyntaxError: unmatched ')'

The unmatched ')' is saying we can not find a opening '(' match for the closing ')' on line 4. In this case we have an extra parenthesis. Delete the ending parenthesis and let's move onto the next error.

After removing the ')' you may notice a lot of your program has red squiggly lines below, this indicates an error. Run the program again and we will see our newest error is at

Traceback (most recent call last):
File "main.py", line 2, in <module>
age == 25
NameError: name 'age' is not defined

We are able to see right away the error is on line 2 and it says age is not defined. This may not be as obvious since we have not discussed it, but there is a difference between the use of = and ==, where one equal sign is an assignment operator saying to store the data we assign to a var and then the double equals operator is a comparison operator where we would say if x is equal to y do stuff. We will learn about conditional statements soon. For the moment adjust age to be age = 25 and rerun the program.

The last error we encounter is another NameError. This is because programming languages are sensitive x and X are not the same if they were variables. So, in our print function we call for a variable named Firstname, while we wrote our variable name as firstName. Once we correct this the program should run, but we haven't produced the exact output we are aiming for. Still the program reads "Betais" instead of "Beta is ". To change this add a space before is in your print function and rerun the program. The final code and results should look like the below.

Hello, Beta is 25 years old.

Lesson 1.05 - Modules, Classes, functions and calling them


We have briefly discussed what a class and function are. Now let us discuss learning how to make a module, import it and create a class so we can begin to call our own functions. In your file interface click the “Add file” button and name the file whatever makes sense to you. In this case I will be labeling the module as lessons.py. We must remember the ‘.py’ extension otherwise it will be a standard empty text file. Open creation we are going to want to make a class to import. You will type

What we are doing here is stating we want to build a class (a means of building data & functionality together) using class. When we create a class we are creating a new type of object, allowing for new instances of that type to be made. Following the identifier we have the class name lessons which remember, can be changed to anything you like that makes sense to you. Then, as a parameter we pass object to inherit all the properties of a possible object. The object is basically the mother of all classes, which needs to be passed as an argument/parameter to allow for the creation of an object to be made.

Tabbed inside of our class like our comment above we are going to create a function/method. To define a function we will write

Please understand all functions need to be tabbed inside the class you are writing in. Otherwise you will come across this error below:

IndentationError: expected an indented block

Pretty easy error to understand. When you read the traceback you can see which line had the indentation error and then easily just press tab to indent the line.

In development.

With no preview button on repl.it, I was unsure how the styling came out, and wanted to check it. As well as I am at work. I hope this guide helps you out if you are new. I am a hobbyist developer and feedback is welcome and if something is inaccurate to please leave a comment and we will update this guide. I hope to make up to 3 Lessons and get the user to be able to learn to read Documentation, create objects and make their own program.)

Voters
Ultro43
DynamicSquid
Zeune
Comments
hotnewtop
HahaYes

hmmmmm the community is trying to take down my fame... jk good tutorial

DynamicSquid

Sweet!