DEV Community

Samwel Njihia
Samwel Njihia

Posted on

Python 101: Ultimate Python Guide or Introduction to Modern Python

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or

age:int = 56 #age will be intenger
height:str = 10 #height will be string
Enter fullscreen mode Exit fullscreen mode

Data type that a function return can be specified as shown

def hello() ->int:
return 6
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

Multi-line Comments

"""
This is a multline
comment
"""
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 )
Enter fullscreen mode Exit fullscreen mode
a) Sets Union (|)

This combination of everything in the sets

lettersA = {"A", "B", "C", "D"}
lettersB = {"E", "F"}
print(lettersA | lettersB )
Enter fullscreen mode Exit fullscreen mode

Results:

{'A', 'D', 'C', 'E', 'B', 'F'}
Enter fullscreen mode Exit fullscreen mode
b) Sets Intersection(&)

This what is common between the given sets

lettersA = {"A", "B", "C", "D"}
lettersB = {"B", "G"}
print(lettersA & lettersB )
Enter fullscreen mode Exit fullscreen mode

Results:

{'B'}
Enter fullscreen mode Exit fullscreen mode
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 )
Enter fullscreen mode Exit fullscreen mode

Results:

{'C', 'A', 'D'}
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode

Results:

john
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

results

('apple', 'banana', 'cherry')
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

If Statement

Executes is the given condition is true

a = 4
b = 10
if b > a:
   print("B is greater than A")
Enter fullscreen mode Exit fullscreen mode

Results:

B is greater than A
Enter fullscreen mode Exit fullscreen mode
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")
Enter fullscreen mode Exit fullscreen mode

Results:

B is not greater than A
Enter fullscreen mode Exit fullscreen mode

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 ")
Enter fullscreen mode Exit fullscreen mode

Results:

Number is equal to 0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Results:

1
2
3
4
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
apple
banana
mango
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Results

Hello code Gurus
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

Results:

Hello code Gurus
Enter fullscreen mode Exit fullscreen mode

Top comments (0)