cft
Become a CreatorSign inGet Started

Introduction To Modern Python

An introduction to the basic concepts of modern python programming in accordance to PEP-8 guidelines.


user

Muriithi Gakuru

3 months ago | 3 min read
Follow

introduction-modern-python-jhxai

What is Python

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects. That's according to our good friend Wikipedia.
Python is best used for automation. It's uses extend to Web, Mobile and Game Development, Machine Learning and Data Science. This is because python can be ran on multiple platforms; windows, linus and mac os.

Getting Started

First things first.
We'll start by downloading a python interpreter for our machine by navigating to
here. Funny story, I had written down how to configure your development environment and installation process but decided it wasn't comprehensive enough and would make this lengthy. Please refer here then come back for an in depth introduction.

Writing your first program

We'll start with the ceremonial starter program, Hello World.
You should be able to work on the command prompt REPL(Read–eval–print loop) environment
This is what I mean.

Hello World

Type a print statement to print.

print('Hello World')

# This is a comment

Python is simple like that.

Comments

# This is a single line comment

"""

This is a multi-line comment

"""

Variable declaration

We declare variables to reuse them later right? Python requires us to assign a value to the variable. You don't need a type identifier since python detects it right away.

my_name = "Muriithi Gakuru"

The snake case is more preferred. This is done by joining variables with an undercase as done above.

Variable types in python

Strings - Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello world' is the same as "hello world"

Numbers - There are three numeric types in python;

  • Integer - is a whole number, positive or negative, without decimals.

a = 251

  • Float - is a number, negative or positive containing one or more decimal points.

b = 19.5

  • Complex - these are written with a "j" as the imaginary part.

c = 15j

  • Booleans - These store an output of either True or False.

Lists

Python lists are used to store data in array format. Different data types can be stored in the same list.

my_list = ['name', 210, 'age', 33.5]

You can call a list's contents using their indices
my_list[0] will give an output of the string 'name' at index 0.

Tuples

Tuples store data much like lists only that values in a tuple are unchangeable. A tuple is ordered and enclosed in round brackets. They are mostly used to store sensitive data that should not be reassigned later in the program such as user information.

my_tuple = ("Ken", 23)

print(my_tuple)

Sets

They are used to store multiple items in a single variable. Sets contain unique items and there cannot be any duplicates or else they will be eliminated.
Set contents are unchangeable but you can remove or add items to the set. They are enclosed by curly braces.

my_set = {"apple", "banana", "cherry"}

print(my_set)

Dictionaries

Dictionaries store data in key:value pairs. They are enclosed in curly braces. They do not allow duplicates since a value can only be accessed using a single, distinct key. dict is a keyword that refers to a dictionary.

my_dict = {

"brand": "Ford",

"model": "Mustang",

"year": 1964

}

print(my_dict)

print(my_dict.keys())

print(my_dict.values())

print(my_dict.items())

# try these out

Loops

Loops take care of indentation according to PEP8

  1. If - Else
    Python supports all logical operations in mathematics
    Equals:
    a == b
    Not Equals:
    a != b
    Less than:
    a < b
    Less than or equal to:
    a <= b
    Greater than:
    a > b
    Greater than or equal to:
    a >= b

b = 13

c = 300

if c > b:

print("c is greater than b")

else:

print("The else clause was called")

  1. While Loop
    This executes a list of statements provided the condition is true.

i = 1

while i < 6:

print(i)

i += 1

# Note: if i is not incremented, the loop will continue infinitely.

  1. For Loop
    For loops are used for iterationg through a sequence. This may be a list, dictionary, set or tuple.

fruits = ["apple", "banana", "cherry"]

for x in fruits:

print(x)

This prints the contents of fruits one after the other.
You may also iterate through a string.

Functions

A function is a block of code that only runs when it's called.
Functions enable the resuse of repetitive parts of the program.
Below is a function to print a user's name.

def print_name():

my_name = "Muriithi Gakuru"

print('My name is ', my_name)

# functions are called by typing the function name and parentheses

print_name()

A function can take an argument or two so as to receive data dynamically later in the program.

def print_name(my_name):

print('My name is ', my_name)

# the argument is placed between the parentheses

# call the function by adding the argument in the function

print_name("Muriithi Gakuru")

All these take practice to have them at your fingertips. These are the fundamentals of modern python. I hope you had fun in the journey of being a techie.
Cheers

Upvote


user
Created by

Muriithi Gakuru

Follow

Software Developer

I am a Software Developer and student at the East African University. I love code and building software solutions. I am the Department leader for Machine Learning and Data Science department on campus


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles