Python For Beginners

Python For Beginners

Lets write our first Python program

competent coders
Source: https://knowyourmeme.com/photos/821695-boardroom-suggestion

There is one big problem with programming courses for beginners.  They guide you trough the long introductions, history facts, lots of theory.

Usually, most people simply quit before getting to the “good stuff”.

Let us try a bit different approach.

 

Lets write our first Python program

Here is a list of things that you need to know for now:

  • There is a lot of programming theory around Python and you will learn it in time.
  • Python has 2 major versions (Python 2x and Python 3x) and you will learn the differences in time.
  • You will have to setup development environment for Python.
  • You have fantastic Python documentation at https://docs.python.org/3/tutorial if you prefer book like learning.

For now, let us postpone most of the standard steps and simply write and run our first Python program.

 

Task: Write a program which reads input number and tells us if that number is positive (bigger than 0) or negative (less than 0).

Hints:

  • There are online Python compilers, which allows you to write Python code in your browser and execute it. Yes, you don’t have to setup anything at this moment. We can simply get started.
  • Try not to do copy paste, because you want to get used to typing, also known as coding… 😉
  • Python cares about indents (tabs). When typing examples, pay attention to this.

Open Python Compiler at this link: https://www.onlinegdb.com/online_python_compiler

Once you open this link, type in the solution as provided here.

x = int(input("Please enter an integer: "))

if x < 0:
    print('The number you have entered is negative')
elif x == 0:
    print('The number you have entered equals zero')
else:
    print('The number you have entered is positive')

Final solution should look like this:

python compiler example
python compiler example

Once you successfully typed the solution in Python compiler, click Run.

python compiler example response
python compiler example response

Program will ask you to enter some number (for example 20). After doing this and clicking enter, program will let you know if the number is positive or negative.

Yay! You just made your first Python program!

Congratulations!

(rainbow unicorns all around, feel that heat of instant gratification, etc, etc.)

Now, what does this program actually do?

For now, just notify couple of things:

  • input() – this is a Python function that is used to get the input from the console. Meaning, it allows someone to type in something and send it to your program. Later, program decides what to do with this.
  • int() – function which tells program to treat some information as integer (otherwise program wouldn’t know how to treat something)
  • int(input(“Please enter an integer: “)) – get something from console and treat it as an integer
  • x = – here ‘x’ represents a ‘variable’. Variables are used to store some information in memory and store it under some name (variable name)
  • x = int(input(“Please enter an integer: “)) – get something from console, treat it as an integer and store it under a name ‘x’
  • if x < 0: – In most of the programming languages IF is a standard way to make a condition. If some condition is good, do some logic..
  • elif – If the first condition is not good, check some other condition.
  • else – If none of the previous conditions were good, do this anyway.
if x < 0:
    print('The number you have entered is negative')

This part of the code says: If the value stored in ‘x’ is smaller then 0, then print out “The number you have entered is negative” message in the console.

elif x == 0:
    print('The number you have entered equals zero')

This part of the code says: If the first condition is not good, and the X is equal to 0, then print out the message in the console.

else:
    print('The number you have entered is positive')

The last part of the code says: If any of the previous conditions were not good (so, X is not smaller than 0 and it is not 0), then print out “The number you have entered is bigger then zero”, which is the only possible option.

That is it. You coded your first Python program.

 

2 thoughts on “Lets write our first Python program”
Leave a Comment