Python 101: Ultimate Python Guide or Introduction to Modern Python
Samwel Njihia
Posted on February 13, 2022
Introduction
Python is a dynamically typed language, i.e., the data type of any variable or function is checked at run time, and due to this, python does not require the definition of data type as in java(Although you can define your data type in python which is not a must). Python is applied in fields such as web development, game development, AI and Machine learning, software development, etc. The article discusses python's basics and how to get started with the python programming language.
Variable Declaration
Variables are containers for storing values of the data.
Example of how to declare and assign values to variable
name = "John"
age = 45
As seen above there is no data type for each variable but data type for each variable can be done as shown
age = int(56) #age will be intenger
height = str(10) #height will be string
or
age:int = 56 #age will be intenger
height:str = 10 #height will be string
Data type that a function return can be specified as shown
def hello() ->int:
return 6
NB Declaration of datatype is not mostly done
Comments
Comment are not executed and they are for code explanation
Single Line Comments
# put comment here
Multi-line Comments
"""
This is a multline
comment
"""
Data Types
These include int, str, boolean and we have already seen how you can specifies them if you want
Data Structures(Collections/Arrays) in Python
These are dictionaries, list, tuples, and sets.
Lists
- Elements are ordered, changeable and allow duplicates
- Items can be added or removed
- Defined using [] or the list() constructor
- Use dot(.) to get in-build method to perform sort, remove etc
numbers = [10,23,90] #declaring using []
num =list((1, 4, 6)) #list constructor, note the double brackets
Sets
- Unordered, unchangeable, unindexed, and do not allow duplicate values
- Items in a set cannot be accessed using index but can be accessed using loop
num_set = {1, 3, 6, 70}
print(num_set )
a) Sets Union (|)
This combination of everything in the sets
lettersA = {"A", "B", "C", "D"}
lettersB = {"E", "F"}
print(lettersA | lettersB )
Results:
{'A', 'D', 'C', 'E', 'B', 'F'}
b) Sets Intersection(&)
This what is common between the given sets
lettersA = {"A", "B", "C", "D"}
lettersB = {"B", "G"}
print(lettersA & lettersB )
Results:
{'B'}
c) Sets Difference(-)
Deference A-B means what is in A and not in B
lettersA = {"A", "B", "C", "D"}
lettersB = {"B", "E", "F"}
print(lettersA - lettersB )
Results:
{'C', 'A', 'D'}
Dictionaries
They are unordered mutable python container that stores mappings of the unique keys to values
- They are ordered , changeable, and do not allow dublicates(from python 3.7 and above)
student = {
"name" : "john",
"age" : 34,
"address" : "US"
}
print(student["name"])
Results:
john
Tuples
- Items are ordered, unchangeable, and allow duplicates values
- Items can be accessed by referring to the index number, inside square brackets
names= ("apple", "banana", "cherry")
print(names)
results
('apple', 'banana', 'cherry')
Conditional Statements
If Statement
Executes is the given condition is true
a = 4
b = 10
if b > a:
print("B is greater than A")
Results:
B is greater than A
If ELse Statement
if executes when given condition is true and else executes when the condition fails
b = 4
a = 10
if b > a:
print("B is greater than A")
else:
print("B is not greater than A")
Results:
B is not greater than A
if---elif--else
used to express multiple conditions
num = 0
if num > 0:
print("Number Positive")
elif num == 0:
print("Number is equal to 0")
else:
print("Number is negative ")
Results:
Number is equal to 0
Loops
These are the while and for loops and they execute as long as the given condition is true
The while loop
The following executes as long as i is < 5
i = 1
while i < 5:
print(i)
i += 1
Results:
1
2
3
4
For loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
The following will print each fruits
fruits = ["apple", "banana", "mango"]
for x in fruits:
print(x)
apple
banana
mango
Functions and Function Call
Function is a block of code that will executes when it is called
def helloWorld():
print("Hello code Gurus")
helloWorld()# function Call
Results
Hello code Gurus
Passing Parameters and Arguments to Functions
- Parameters are variables in method while arguments are data passed to the method's parameters
- Arguments is the actual value of the variable that get passed to function
def helloWorld(name):
name = 'Hello code Gurus'
print(name)
helloWorld(name)
Results:
Hello code Gurus
Posted on February 13, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.