Introduction to Python

A brief introduction to Python programming for beginners.

Besmire Thaqi
Python Pandemonium

--

Photo by Unsplash on Unsplash

Besides being a family of nonvenomous snakes, Python is also a high-level scripting/programming language. It was created by Guido van Rossum and released in 1991. It is widely used from developers for different purposes.

Why do developers choose to work with Python?

The best thing about Python is that you can get things done with less code comparing to other languages. It has a very simple syntax, and runs on an interpreter system, meaning that code can be executed as soon as it is written. It can be treated in an object-oriented way and functional way. Python can be used for web development (server-side), software development, data science, AI & machine learning, solving complex mathematics and more.

Most of the biggest companies use Python for numerous reasons. It’s worth mentioning that Python is Google’s first choice and most of Google’s tools are developed with this language. Facebook, Quora, Dropbox, Instagram, Spotify, Netflix, Reddit and more, have dedicated developers and engineers focused in Python and its frameworks.

Starting with Python

The goal is getting to know the syntax and the basics of this language. Let’s dig into a little bit of instructions and code!

1.0 Installation

If working in Linux or MacOS, then Python is already pre-installed there. But, if working in Windows, it needs be installed. Follow instructions here.

1.1 Editors and IDE

Any editor can be used by choice like Visual Studio Code, Brackets, Atom etc. Some of the specified ones and IDEs for Python are PyCharm IDE from JetBrains, Jupyter, and Spyder.

1.2 Variables and types

In Python, there is no need to declare variables or their type before using them. Regarding the numbers, it supports different types of numbers: integers (whole numbers), floats (decimals) and complex numbers.

# define an integer
myint = 1
# define a float
myfloat = 1.2

Strings are defined either with single quotes or double quotes.

# define a string with single quotes
mystring = 'Hello world!'
# define a string with double quotes
mystring = "Hello world!"
# double quotes make it easy do use apostrophe in the string
mystring = "Don't mind the apostrophe here!"
# string formatting, gets the value of defined variables
name = "Jane"
age = 20
print("My name is %s and I'm %s years old." % (name, age))

Simple operators can be also used in numbers and strings as well.

two = 2
four = 4
print(two+four) # returns 6
two = "two"
four = "four"
print(two+" "+four) # returns two four
two = 2
four = "four"
print(two+four) # returns type error because it's unsupported

1.3 Lists

Lists are basically like arrays. You can add as many elements as you want and you can iterate through the list.

# define a list
mylist = []
# add elements to the list
mylist.append(1)
mylist.append(2)
mylist.append(3)
# print the first element in the list
print(mylist[0]) # returns 1
# iterate through the list
for element in mylist:
print(element) # returns 1 2 3 each in new a line

1.4 Loops

The example above shows how to iterate through a list. In Python it is also supported to iterate through elements with “range” function.

for i in range(3):
print(i) # returns 0, 1, 2
for i in range(3, 5):
print(i) # returns 3, 4
for i in range(3, 10, 3):
print(i) # returns 3, 6, 9

“While” loops are also supported together with “break” and “continue”. The “break” is used to exit a loop and “continue” is used to skip the current block and return to the “for” or “while” in the beginning.

# while loop repeats as long as a condition is met
count = 1
while count < 3:
print(count) # returns 1, 2
count += 1 # raises the count variable for one
# break condition in a loop is used to exit the loop
count = 1
while True:
print(count) # returns 1, 2, 3
count += 1
if count > 3:
break # exits when the count is already 4
# continue condition is used to skip the current block
for i in range(8):
if i % 2 == 0: # checks if number is even in order to skip it
continue
print(i) # returns only odd numbers 1, 3, 5, 7

1.5 Conditions

The boolean logic is used to evaluate conditions. Therefore; True or False is returned when evaluating or comparing conditions.

i = 4
print(i>5) # returns False
print(i==5) # returns False, two equal operators to check it's equal
print(i<5) # returns True
print(i!=5) # returns True, != operator to check if it's not equal

There are boolean operators (“and” or “or”) supported together with “in”, “is”, “not” operators.

first = 1
second = 2
if first == 1 and second == 2:
print("First and second number is 1 and 2.")
if first == 1 or one == 0:
print("First number is either 1 or 0.")
if first in [0, 1, 2]:
print("First number exists in the list.")

Unlike the “==” operator, there is “is” operator that does not match the value of variables but the instances themselves. And the “not” condition checks the negotiation.

i = [0, 1]
j = [0, 1]
print(i==j) # returns True
print(i is j) # returns False
print(not False) # returns True

1.6 Functions

Functions are a convenient way to divide code into useful blocks, therefore; making it more readable and be able to reuse the code.

def my_function():
print("Here's a function!")
my_function() # returns Here's a function!def my_function_with_args(name, age):
print("%s is %s years old." % (name, age))
my_function_with_args("Jane", 20) # returns Jane is 20 years old.

1.7 Classes and objects

An object is a unique instance of a data structure defined by a class. A class is like a template to create objects. Basically an object is an instance which is a copy of a class with actual values.

class MyClass:
name = "Jane"
def my_function(self):
print("A function inside a class")
# creating an object/instance of the class
my_object = MyClass() # holds an object of the class MyClass
# accessing the variables and functions defined in the class
print(my_object.name) # returns Jane
print(my_object.my_function()) # returns A function inside a class
# multiple objects can be created from the same class
my_object_2 = MyClass()
print(my_object_2.name) # returns Jane
print(my_object_2.my_function()) # returns A function inside a class

1.8 Dictionaries

A dictionary is a data type similar to arrays. The difference is that arrays use indexes, and dictionaries use keys and values. Each variable stored in a dictionary can be accessed using the key. A key can be any type like string, number, list, etc.

fruits = {}
fruits["apple"] = 10
fruits["banana"] = 20
fruits["pear"] = 50
print(fruits) # returns {'apple': 10, 'banana': 20, 'pear': 50}# accessing the variable with the key "apple"
print(fruits["apple"]) # returns 10
# iterating through the dictionary
for fruit, amount in fruits.items():
print("Fruit and amount:", fruit, amount)
# returns as shown below:
# Fruit and amount: apple 10
# Fruit and amount: banana 20
# Fruit and amount: blueberries 50

So far we have covered the syntax and the basics of Python like variables, operators, lists, conditions, functions, classes and objects, and dictionaries. To learn more and dig deeper, practice is required of course, and many other sources for tutorials and books can be helpful too.

Books and sources to learn

First, there are great forums and communities to join and visit such as PyForum. Then, also any course or tutorial that doesn’t cost much and covers intro in Udemy or Coursera is highly recommended.

Different sources that offer introductions (for free) and tutorials such as:

Some of the books for Python beginners that have good reviews:

  • Python Crash Course by Eric Matthes (No Starch Press, 2016)
  • Head-first Python, 2nd edition by Paul Barry (O’Reilly, 2016)
  • Think Python, How to think like a Computer Scientist, 2nd edition by Allen B. Downey (O’Reilly, 2015)
  • Learn Python 3 The Hard Way by Zedd A. Shaw (Addison-Wesley, 2016)

A short recap

A brief introduction to Python programming language for beginners explains where this language comes from, why is it widely popular and who uses it for development. Understanding the syntax and the basics have been covered and explained with examples using pieces of code.

Moreover, the beginner’s guide and a developer’s will to learn does not stop here. There are really good sources like books and tutorials (cheap and free) that can be helpful to read and follow.

--

--