DEV Community

Billy Okeyo
Billy Okeyo

Posted on • Edited on

1 1

Introduction to Python Programming Basics(Part 1)

Variables

A variable is a container for a value and it can be of various data types.

Variable Rules

  • Variable names are case sensitive
  • Variable names must start with a letter or underscore
  • Variables can have numbers but cannot start with numbers

Data Types 

int —  for numericals

string — for alphanumericals

float — for numerics with decimal points

boolean — true or false 

x = 1 #int
y = 1.22 #float
name = "Billy" #string
is_cool = True #boolean
#x, y, name, is_cool = (1, 1.22, "Billy", True)
print(x, y, name, is_cool)
view raw Variables.py hosted with ❤ by GitHub

Strings

Strings in python can be written with either single or double quotes 

Lists

A list is a collection which is ordered and can be changed.
A list also allows duplicate members.

To create a list we usually use square brackets or we can use a constructor as shown below;

numbers = [1, 2, 3, 4, 5]
names = ["Billy", "Dan"]
#Get a value from list
print(numbers[2])
#Append to list
names.append("Okeyo")
print(names)
#Removing from list
numbers.remove(5)
print(numbers)
#Insert at specific position
numbers.insert(3, 10)
print(numbers)
#Remove with pop
numbers.pop(3)
print(numbers)
#reverse list
names.reverse()
print(names)
#Adding a duplicate
names.insert(2, "Dan")
print(names)
view raw Lists.py hosted with ❤ by GitHub

Tuples

A tuple is a collection which is ordered and cannot be changed.
A tuple also allows duplicate members.

In creating tuples we use parenthesis

numbers = (1, 2, 3, 4, 5)
names = ("Billy", "Dan")
print(type(names))
#When declaring one value in a tuple you should add a comma after it
campus = ("Thika")
print(type(campus))
campus = ("Thika",)
print(type(campus))
#Let's try changing a Value
#names[0] = "Okeyo"
#deleting a tuple
#del numbers
#print(numbers)
view raw Tuples.py hosted with ❤ by GitHub

Sets

A set is a collection which is unordered and unindexed which means when I print a set now it won’t be in the same when i print it again.
A set doesn’t allow duplicate members
names = {"Billy", "Dan"}
print(names)
#Check if a value is in a set
print("Billy" in names)
#Add to set
names.add("Okeyo")
print(names)
#Remove from set
names.remove("Dan")
print(names)
#Clearing a set
#names.clear()
#print(names)
#Let's try adding a duplicate
names.add("Billy")
print(names)
view raw Sets.py hosted with ❤ by GitHub

Dictionaries 

A dictionary is a collection which is unordered, indexed and changeable.
A dictionary doesn’t allow duplicate members.
# A dictionary is unordered collection, changeable and indexed, it doesn't allow duplicate members
student = {
"Name": "Billy",
"Reg No": "BIT/001",
"Age": 50
}
print(type(student), student)
# Get a Value from dictionaries
print(student["Name"])
print(student.get("Reg No"))
# Adding values
student["Course"] = "BIT"
print(student)
# Getting Dictionary Keys
print(student.keys())
# Coping a Dictionary
student1 = student.copy()
student1["Year"] = "2020"
print(student1)
# Dictionary List
lecturers = [
{"Name": "John", "Age": 50},
{"Name": "Doe", "Age": 30}
]
view raw Dictionary.py hosted with ❤ by GitHub

Functions

A function is a block of code which executes upon calling 
Python is very sensitive to indentions and thus in creating functions, we make use of indents.

In creating functions we use the def keyword, as shown below

def user(name):
print(f"Hi {name}")
print("It is a good day to learn Python")
user("Billy")
view raw function.py hosted with ❤ by GitHub

 

That’s all for now, I’ll be posting a continuation on this later on.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (1)

Collapse
 
hobbolt profile image
HOBBOLT

Billy!
As i learn python, would you be willing to help me develop an app? I am so new to coding.