Python Basics (A Guide for beginners)

A beginners guide to understanding Python

What is Python?

Python is an easy-to-learn popular high-level programming language created by Guido van Rossum in the late 1980s. Python's syntax is designed to be easy to read and write, making it a popular choice for beginners. Python's popularity and ease of use have made it a popular choice for programmers and data scientists.

Just like any other programming language, Python has syntax, variables, and data types which are the topics we would touch on here.

Syntax

Syntax defines a set of rules that are used to create Python statements while writing a Python Program.

print ("Hello, World!")
#output is Hello World.

Variables

A variable is a container for holding data that can be manipulated and accessed within a program. Variables allow you to store different types of data such as numbers, strings, lists, or even more complex objects.

A Python variable is created when you assign a value to it. The equal sign (=) is used to assign values to variables. Items on the right are the values assigned to the variable created on the left.

age = 30          #Creates an integer variable
amount = 1000.0       #Creates a floating point variable
name = "Robbie Akoto"   #Creates a string variable

#"age", "amount", "name" are the variables created while the "30", "1000.0", "Robbie Akoto" are the values assigned.

To print out the above, all you need to do is use the syntax print like below

age = 30         
amount = 1000.0      
name = "Robbie Akoto" 

print(age)
print(amount)
print(name)  
#output are "30", "1000.0" and "Robbie Akoto" respectively.

Deleting variables

In Python, it is possible to delete the reference to a variable by using the del statement. Let's look at an example:

details = {1:"Robbie", 2:"Akoto", "age":30}
print(details)

del details
print(details)

this throws an error result like so:

{1: 'Robbie', 2: 'Akoto', 'age': 30} 
Traceback (most recent call last): 
    File "python.py",line 15, in print(dictionary) 
NameError: name 'dictionary' is not defined

You can use del to remove slices from a list like so:

del details[1] #this removes "Robbie" from details

Multiple Assignments

Python allows you to assign a single value to several variables simultaneously. This means you can create multiple variables at a time. In short, Python is smart about variable assigning. An example is:

num1,num2,name = 1,2,"Robbie Akoto"

print (a)
print (b)
print (c)

#result
1
2
Robbie Akoto

Every Python variable should have a unique name. Also, there a rules to naming variables which you can read more here.

Data Types

Just like any other programming language, data Types are used to define the type of a variable in Python. It defines what type of data we are going to store in a variable. There are two categories Mutable(can be modified after creation) and Immutable( cannot be modified after creation):

  • Integers, floats, booleans, strings, tuples, frozen set (Immutable)

  • Lists, dictionaries, and sets (Mutable)

Let's look at a few:

  1. List: A list is an ordered collection of similar or different types of items separated by commas(,) and enclosed within brackets [ ].

Example:

 details = ["Robbie", "Akoto", 1, 2, 3]
  1. Dictionary: A dictionary is associative arrays in other languages. They are key-value pairs created with curly braces { }. It is very useful to retrieve data in an optimized way among a large amount of data. Placing a comma-separated list of key: value pairs within the braces adds initial key: value pairs to the dictionary; this is also the way dictionaries are written on output.

Example:

 dict = {1:"Robbie",2:"Akoto", "age":30}
 dict [3] = "Teddy" #adds a new record to the variable set.
 del dict['age'] #removes the age record
  1. Set: A set is an unordered collection of unique elements that duplicate values are not allowed.

Example:

 orders = {1, 2, 3, 4}
  1. Tuple is an ordered sequence of items same as a list. Tuples once created cannot be modified.

In Python, we use the parentheses () to store items of a tuple. For example,

product = ('Ipad', 499.99) #creates a tuple

print(product[0]) #access an item in the tuple

Here, the product is a tuple with a string value Ipad and value of 499.99.

  1. A string is a sequence of characters represented by either single or double quotes.

Example:

name = "Robbie Akoto"
print(name)

#output
Robbie Akoto
  1. Numbers are data types that store numeric values like float, integer, and complex.

Example:

#Integer:data type used to represent positive or negative numbers
number1 = 10
print(number1)

#Float:data type used to represent floating decimal numbers
number2 = 2.0
print(number2)

#Complex:holds complex numbers and character "j" as an imaginary part
number3 = 1+2j
print(number3)
  1. Boolean represents either true or false.

Example:

number1 = 10
number2 = 20

print(number2 < number1) 
#output: false

Although this is not in-depth Python, this should give you a deep understanding of what Python can do and what you should know upfront even if you are new to programming.

Thanks for reading, and happy learning Python!

Let's continue to learn and grow together. ✍️

Don't forget to like, share, and comment your thoughts 😊.