Introduction to Python

Python is an interpreted high-level programming language for general-purpose programming.

Python logo

Python syntax is very clean, with an emphasis on readability and uses standard English keywords.

Who uses Python?

Python is used by many large organizations like Google, YouTube, Quora, Instagram, Netflix, and NASA. The list goes on.

Okay, what I can start building in python?

Pretty much anything you want. For e.g

  • GUI application.
  • Create Websites.
  • Analyse Data.
  • System Administration.
  • Game Development.
  • Machine Learning
  • Internet of Things
  • Web Scrapping

and many more …

How to install Python?

  • You can download Python directly from its project site and install individual components and libraries you want
  • Alternately, you can download and install a package, which comes with pre-installed libraries. I would recommend downloading Anaconda.

Getting started with Python

You can use variables if you need to but you can even use it like a calculator. For example:

>>> 1 + 2
3
>>> name = "Sarah"
>>> "Hello " + name
'Hello Sarah'

Basic Python usage

Hello world in Python:

print("Hello world")

Simple as that!

INDENTATION

Some languages use curly braces { and } to wrap around lines of code which belong together and leave it to the writer to indent these lines to appear visually nested. However, Python does not use curly braces but instead requires indentation for nesting. For example a for loop in Python:

for i in range(10):
    print("Hello")

The indentation is necessary here. A second line indented would be a part of the loop, and a second line not indented would be outside of the loop. For example:

for i in range(2):
    print("A")
    print("B")

would print:

A
B
A
B

whereas the following:

for i in range(2):
    print("A")
print("B")

would print:

A
A
B

VARIABLES

To save a value to a variable, assign it like so:

name = "Bob"
age = 15

Note that data types were not specified with these variables, as types are inferred, and can be changed later.

age = 15
age += 1  # increment age by 1
print(age)

This time I used comments beside the increment command.

COMMENTS

Comments are ignored in the program but there for you to leave notes, and are denoted by the hash # symbol. Multi-line comments use triple quotes like so:

"""
This is a very simple Python program that prints "Hello".
That's all it does.
"""

print("Hello")

LISTS

Python also has lists (called arrays in some languages) which are collections of data of any type:

numbers = [1, 2, 3]

Lists are denoted by the use of square brackets [] and each item is separated by a comma.

ITERATION

Some data types are iterable, which means you can loop over the values they contain. For example a list:

numbers = [1, 2, 3]

for number in numbers:
    print(number)

This takes each item in the list numbers and prints out the item:

1
2
3

Note I used the word number to denote each item. This is merely the word I chose for this – it’s recommended you choose descriptive words for variables – using plurals for lists, and singular for each item makes sense. It makes it easier to understand when reading.

Other data types are iterable, for example, the string:

var_name = "INDIGO"

for char in var_name:
    print(char)

This loops over each character and prints them out:

I
N
D
I
G
O

RANGE

The integer data type is not iterable and trying to iterate over it will produce an error. For example:

for i in 3:
    print(i)

will produce:

TypeError: 'int' object is not iterable

Python error

However, you can make an iterable object using the range function:

for i in range(3):
    print(i)

range(5) contains the numbers 0, 1, 2, 3 and 4 (five numbers in total). To get the numbers 1 to 5 (inclusive) use range(1, 6).

LENGTH

You can use functions like “len” to find the length of a string or a list:

name = "Jamie"
print(len(name))  # 5

names = ["Bob", "Jane", "James", "Alice"]
print(len(names))  # 4

IF STATEMENTS

You can use if statements for control flow:

name = "Joe"

if len(name) > 3:
    print("Nice name,")
    print(name)
else:
    print("That's a short name,")
    print(name)

Executing Python files from the command line

You can write a Python file in a standard editor like Vim and run it as a Python script from the command line. Just navigate to the directory the file is saved in (use cd and ls for guidance) and run with python3, e.g. python3 hello.py.

Python command line

Credits:

 https://www.raspberrypi.org/documentation/usage/python/

https://www.python.org/

Please signup for regular updates. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s