Programming with python

Updated:

My first writing with python code happens when I was in college. School year 2010 with basic fundamentals.

Python syntax is pretty straight forward which makes it easy to learn compared to other programming languages. No semicolon (;) as terminator at the end of statement. For example:

print("hello world")

Python is an interpreted, high level, general-purpose programming language created by Guido van Rossum. The language emphasizes code readability.

First line of program

Just like any fresh learner when learning any programming language. The first thing to do is to print "Hello World". A successful printing of "Hello World" indicate the successful setup and installation of the programming language in the system unit.

To print "Hello World" just type:

print("hello world")

And it will display "Hello World" in the console.

Requirements

For this article I recommend using pycharm as editor because it's basically created for programming using Python. Although it's possible to write and run python using  favorite editor like vscode, atom, etc. Also in case  you never know Python was pre-installed in an operating systems like Windows. In Ubuntu, just type python2 --version in the terminal and it will give the installed version.

A latest version can be downloaded also at their official website at python.org.

print("hello world")

Setup and running

If using pycharm you can start with creating a new python file

and to run a program click the play button at the top-right of the editor.,

That's it. Really simple.

Printing a simple shape

To print a simple shape can be done by writing this in editor

print("   /|")
print(" / |")
print(" / |")
print("/___|")

And it will display

   /|
/ |
/ |
/___|

in the console. What python does, it reads the instruction line by line arriving the result of a simple right-angle triangle using print() function.

Variables

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory.

Writing a variable in python is a straigthforward also

character_name = "John Doe"

And to print the variable value in the console can be done like

print("There was a man named " + character_name)

Here the value of character_name is concatenated in a phrase "There was a man named " using plus (+) sign.

Note that two or more words of a variable name in python should be separated with underscore (_).

Changing the value of a variable

To change the value of the previously declared variable is by using the equal (=) sign indicating a new assignment of value.

character_name = "John Doe"
character_name = "Jane"

Here the previous value of "John Doe" has changed to "Jane".

Variable datatypes

There are six standard datatypes of variable in python

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary
  6. Boolean

Python developers mostly deal with 3 types of it, that is string, numbers and boolean.

Dealing with string

String in python is any value enclosed with quotation mark (")

"I am a string example"

And there are bunch of stuff to be able to manipulate a string value inside variable. One is the use of \n which tell the interpreter to print the next string to new line. for example

"I am a string nexample"

Would output to

I am a string 
example

Using backslash () will tell the interpreter to escape the next character or perform specific task to manipulate the output of the string. Others can be n, t, and many more.

Using functions

There are also functions available that can be use to manipulate string in a variable.

phrase = "I am the example phrase"
phrase.upper() # print I AM THE EXAMPLE PHRASE
phrase.lower() # print i am the example phrase

There are also functions available that can be use to check the kind string in a variable. It is prefix with .is

phrase.isupper()

Which will evaluate to true or false.

Method chaining

Calling a function one after another can be like

phrase.upper().isupper()

There's also a global function called len() which will determine how many string in a variable.

len(phrase) 

It's also possible to get the individual character of a string in a variable

phrase = "Horse"
phrase[0] # H
phrase[1] # o

The [0] and [1] and called index and enclosing with [] is called array. Index in Python always start with 0.

to be continue.