Python Basics! Python 101!

Python is an incredibly versatile, expansive language which due to its similarity to every day languages, is surprisingly easy to learn among inexperienced programmers. Python is very popular among professionals and students because it has a clear and simple syntax that will get you writing useful programs in short code. It even has an interactive mode, which offers immediate feedback, allowing you to test out new ideas almost instantly and has plenty of libraries allowing programmers to get a lot done with relatively little code.

Some of python's applications include:

  • Python is used in most of the search engines
  • Enables banks to track the money in their accounts
  • Used to program machines that are used to perform tricky medical procedures in hospitals.
  • Desktop applications
  • Web development
  • Automation

Getting started with python

As you can see python is quite handy now let’s get started. To install windows on your system visit the python website. Here and click on the latest version of Python for Windows. The installer file will download automatically. Of the different installer options, select “executable installer”. Double-click the installer file to install Python. Choose “install for all users” and click “next” at each prompt, without changing the default settings.
When the installation is finished, check that it was successful by opening the IDLE program. Go to the “Start” menu, choose “All Apps”, and then select “IDLE”.

On a mac

Go to the Python website.Here and click on “Downloads” to open the download page.From the downloads options, click on the latest version of Python 3 that matches your operating system. The Python.pkg file will download to your Mac automatically. Install python. You’ll find the .pkg file in the “Downloads” folder. Its icon looks like an opened parcel.Double-click it to start the installation. At the prompts, click “Continue” and then “Install” to accept the default settings. Open IDLE. When the installation is finished, check that it was successful by opening the IDLE program. Open the “Applications” folder, and then the “Python” folder. Double-click “IDLE”

Your first program in python

This is now the time to get our hands dirty and what better way than the programmers best friend “hello world”.

First create a new folder where you will be saving your python files, python you wouldn’t want your files flying about the home directory. Then, launch the vs code and locate your new folder.
In Vscode install the python extension and then create a file called hello_world.py, enter the following command and then save.

Print(‘hello World’)

Print () is an in-built python function that displays a message on the screen. In our example it will output ‘hello World’

Executing the python program

To execute our program click the run button and the output will be displayed in the terminal. The message that will be seen on the screen is

hello World

Note that you are more or less familiar with python lets dive into the basic syntax

Python basic syntax

Keywords are words that python reserves for defining the syntax and structure of the python language. Hence, you cannot use them as a regular identifier when naming variables, functions, objects, classes, and similar items or processes. They include but not limited to:

and assert break for not is
lambda else from finally pass for
raise None False del if import

All the keywords except True, False and None are in lowercase and keywords are case sensitive.
An identifier is a name given to a variable, class, function, string, list, dictionary, module, and other objects.

Rules for writing identifiers

  1. Identifiers can be a combination of letters in lowercase or uppercase or digits and an underscore. E.g p, P lowercase, UPPERCASE, python_basics1
  2. An identifier cannot start with a digit.
  3. Key words cannot be used as identifiers.
  4. An identifier cannot contain special characters e.g ‘#@$%
  5. An identifier can be of any length.

Naming Global Variables

Global variable names should be written in lowercase. Names consisting of two or more words should be separated by an underscore.

Naming Classes

The CamelCase convention is used when naming a class.

Naming instance Variables

Instance variables consisting of several words should be separated by an underscore and variable names should be in lowercase

Naming modules and Package Names

Names for modules should be in lower case, they should be short one-word

Naming functions

Function names should be in lowercase and those with several words should be separated by an underscore

Naming Constants

Constants are written in all uppercase letters and underscores are used to separate those with several words

Comments

Comments are notes used to explain the code in the program and cannot be seen by the user running the program but can be seen in the source code. Comments make it easy for you and other programmers to evaluate your work many years after the code was written.

#single line comment
'''
multiline comment
using a doctstring
'''

Variables and Data Types in python

Variables

A variable is a reserved memory location that can be used to store values. This means that when you create a variable you reserve some space in the memory. You can declare a variable using an appropriate name and assigning a variable to it using the assignment operator (=)

student ="John smith"
age = 20
amount = 20.05
paid = False

You don’t need to tell python that a variable should contain a specific type of data. Python automatically identifies the data type based on the values you assign to the variable.

Data types

1). strings
A string is an ordered series of Unicode characters which may be a combination of one or more letters, numbers, and special characters. It us an immutable data type which ,means that when its created it cannot be changed.

single_string = 'this is a single string'

multiline_string ='''
                    like
                   this
                   example
                   '''

#for escaping characters
esc_string = "The teacher said:\ "You are a genuis.\""
print(esc_string) # The teacher said: "You are a genuis"

#concatinate stringg
greet ="hello"
name ="john"
print(greet+ ""+ name)# hello john

#interpolation
# method 1
name = "John"
age ="20"
message= f"{name} is {age} years."

#method 2
name = "John"
age ="20"
message = '{}is {}years.formart(name,age)'

#method 3
name = "John"
age ="20"
message = '{} is {}.format(first=name, last=age)'

#accessing characters in a string
name = "John Smith"
print(name[0])#J
print(name[6:9])#Smith

2.) Number Data types
There are 3 numeric data types in python i.e int, float and complex

normal_integers = 46
floating_point_numbers = 50.04
complex_numbers = 3 +5i

# converting from one numerical Type to Another
float(56)
int(3.0004)
complex(45)

Arithmetic Operators

print(3+2) # 5 (addition)
print(10-5) # 5 (subtraction)
print (3*6) # 18 (multiplication)
print(46/2) # 23 (division)
print(3**3) # 9 (exponent)
print(11%4) # 3 (modulus)
print(60//4) # 15 (floor division)

3). Booleans

d= True
b= False

4). Lists
A list is one of the most commonly used sequences and it is the most flexible type. It can hold one data type or a combination of several data types. A list is mutable that is you can add, remove or modify its elements.

# can create a an empty list with this syntax 
my_list =[]

# lists with integers
num_list = [1,2,3,4,5]

# list with strings
string-list = ["John", "Mary", "Peter"]

# list with mixed data types
mixed_list = ["John", 24, True]

# methods on lists
children = ["John", "Mary", "Sophy", "Peter"]
# append
children.append("Jerry")

# insert
children.insert(2,"Tom")

# remove
children.removed("Sophy")

# get length
print(len(children)) # 5

5). Tuples
Tuples are just the reverse of lists. They are immutable and surrounded by ().

new_tuple  = ("a","b","c","d","e")
print(len(new_tuple)) # 5
print(new_tuple[3]) # d

6). Dictionaries
This is unordered collection of key-value pairs which are separated by a colon , and enclosed in curly braces {}. A dictionary can contain any data type and is mutable however its keys are immutable and can only contain numbers, tuples and strings

ages= {
"John": 20,
"Mary":21,
"Tom":25,
"Jerry":19
}

# access, modify, delete

print(ages[Mary]) # 20
print(len(ages))  #  4

This marks the end of our introduction, I hope by now you know what python is why choose python, applications of python and how to install it on your machine. We have gone through how to save python files and execute them, the basic syntax in python and the various data types. Thank you for reading

14