So you want to learn how to code? You have no prior coding experience or are you coming from another programming language? You have come to the right place. This is an ultimate beginners guide to Programming with Python. We’re not going to go knees deep into programming. But we’re going to get started and write code as quickly as possible. So let’s get started!
Part I: Setting up the coding environment
You can write Python code on your terminal, VSCode or even on a dedicated IDE like PyCharm. But since you’re a beginner we don’t want to you waste time setting one up. You can do it later. For now, you can look to the cloud for an easy and instant IDE setup.
Go to Repl.it. It gives you an instant IDE to learn, build, collaborate, and host all in one place.
- Create a new Repl.
- Choose Python (not Python 2.7).
- Name your Repl.
- Create Repl.
Now that we have an IDE, let’s write our first line of code!
Part II: Let’s Write Some Code!
Before you start coding, in any programming language it’s a tradition to get started by writing code to print “Hello World!” So let’s not break tradition and do just that!
1. Let’s print stuff using Python :
The middle section of the repl is where you write your code. The right side is called a console. Think of it as the middle man between you and python. The console is where you’ll do your input/output to your python program.
To print something on the console, we use the print() statement. Specify the text you want to print inside
‘ ’ or
“ ” quotes.
After writing the above code in the console hit the Run button on top.
Congratulations! You have written your first piece of Python Code! Now let’s go ahead write some code that actually does something.
By now you should be familiar with how to use repl.it. From now I’ll embed the repl directly here in the article so you don’t have to keep switching tabs which can quickly become really annoying. So you can write in your own code and run them right here!
2. Comments in Python
Comments are used to explain what your line of code does. You can write a comment using the ‘
#’ symbol. The ‘
#’ tells python to ignore the line and not to execute it. You can even add
# symbol before existing lines of code to make it a comment. You can also have multi-line comments by starting and ending it with
‘’’. Refer the example below for clarity.
Tap the big green play button below to execute the code. You can also edit the code here. Add in your lines of code and run them!
3. Variables in Python
Think of variables like a box, where you store stuff. You can store numbers, characters, sentences and so much more. For now let’s work with numbers, characters, and sentences(strings).
Let’s assign a number to a variable.
Equal to symbol ‘
=’ is an assignment operator and is used to assign values to variables.
myVariable = 13
myVariable2 = 12.6969
You can go ahead and check the type of your variable using the
type() statement, and passing in your variable name inside of the
type(myVariable2)# To view this in the console pass the above statement inside the print function like this :print(type(myVariable))
When you run the above code you’ll see that
<class ‘int’> and
You might’ve guessed it. It’s because 13 is an integer and 12.6969 is a decimal number which we call it as floating-point numbers in the programming world.
Let’s assign a character/string to a variable.
To assign a character to a variable, you must enclose the value of the variable in
‘ ’ (single quotes) or
“ “ (double quotes).
Characters by convention have a length of one and use
‘ ‘ (single quotes).
Strings are sentences or have more than one character generally enclosed in
“ “ (double quotes).
char1 = 'a'
string1 = "Musk Cult"
Run the code below and see for yourself!
4. Arithmetic Operations in Python
Remember we created number valued variables above? You can perform mathematical operations on them. Let’s see the implementation of some of the basic arithmetic operations in math.
The code below is pretty self-explanatory. Run the code and check it out.
myVariable1 = 6
myVariable2 = 3
Addition + :
myVariable + myVariable2# To see the result on the console :
print(myVariable + myVariable2)
Subtraction - :
print(myVariable - myVariable2)
Multiplication * :
print(myVariable * myVariable2)
Division / :
print(myVariable / myVariable2)
5. Printing variables
Previously we printed the result of the arithmetic operations directly using the print statement. But there’s a better way to do this; store the result in another variable.
result = myVariable + myVariable2
You can also print the values of variables along with a print statement. Let me show you what I’m talking about. Run the code below.
print("The result of addition is :",result)
So instead of having just a number being printed in the console, we can have meaningful statements.
You can also specify a particular place in the print statement where you want your variable to be printed.
print("%s is the value of myVar!" % myVar)
Instead of the comma, we used after the string inside the print statement we use the ‘
%’ here. ‘
%s’ is called a string literal which is used to specify the position inside a string where you want the value of a variable to be.
print("The result of addition of %s and %s is : %s" % (myVar,myVar2,result))
Here we wanted to place multiple variables so, after the
% add a parenthesis
( )and mention the variables in the order you want them inside the print statement separated by a comma. Run the code below and see for yourself.
6. Getting input from the user.
Previously we assigned values to variables ourselves. Instead, we can get the value of the variable from the user. Here’s how :
We use the
input() statement. Equate the variable to an input statement. And inside the parathesis pass in your statement inside single quotes or double-quotes.
myVar = input("Enter the First Number : ")
myVar2 = input("Enter the Second Number : ")
If you check the type of
myVar2 it’s going to be
It’s because by default the type of the input function is a string. But we can perform mathematical calculations only on numbers. So to convert this to a number we do something called a ‘Type Casting’. We wrap the input statement inside an
int(). This converts the string type to integer typer. If you want to work with decimals use
myVar = int(input("Enter the First Number : "))
myVar2 = int(input("Enter the Second Number : "))
And there you have it! You’ve written your first piece of code that does something! We’ll learn more about programming in python in upcoming posts!