DEV Community

Cover image for Python basics 101

Python basics 101

Software Developer I love coding<3 Be cool, Keep coding ;-)
・6 min read

What is python?

Python is a high-level, general-purpose and a very popular programming language. It was created by Guido van Rossum and first released in 1991. Python is dynamically and strongly typed.

Python pros

  • simplicity: high-level programming language has easy syntax. Makes it easier to read and understand the code.
  • Portability: Write once and run it anywhere.
  • Free and Open-Source: This makes it free to use and distribute. You can download the source code, modify it and even distribute your version of Python.
  • Dynamically Typed: Python doesn’t know the type of variable until we run the code.Data types are asssigned at execution

Setup and Running

It can be used across all platforms i.e: Windows, Linux, Mac OS.
Windows: Download python for windows
Linux: Linux systems are pre-installed with python 2.
to upgrade to py3 use below commands;
$ sudo apt-get update
$ sudo apt-get install python3.6

Running python code

After correct setup run command prompt in windows or terminal for linux and type python:
C:users\dev> python
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

>>>print("Hello World!")
>>>Hello World! #this a comment
Enter fullscreen mode Exit fullscreen mode

Voila! You run your first python code in command line. The print() method is used to display on the screen.
python programs are saved with a .py extension, i.e

To execute a program navigate to its path using cmd or terminal in linux/mac os; run this

$ python3
$ Hello World! #gets printed if all is okay
#creating a comment is done with '#' or """..."""
Enter fullscreen mode Exit fullscreen mode

python applications in real world.

Python is the “most powerful language you can still read”.
– Paul Dubois

python application

Variables in python

Python variables are containers for storing data values.

#assigning variables
name = "billy" #type(name)>'str'
age = 23 #type(age) is> 'int'
Enter fullscreen mode Exit fullscreen mode

variable names are called identifiers, and they follow some naming conventions as guided by pep8.

  • Variables can have letters (A-Z and a-z), digits(0-9) and underscores.
  • It cannot begin with an underscore (_) or a digit.
  • It cannot have whitespace and signs like + and -, !, @, $, #, %.
  • It cannot be a reserved keyword for Python like: False, True, for, def, etc.
  • Variable names are case sensitive.
#valid variable names:

#invalid names
Enter fullscreen mode Exit fullscreen mode

more on variables.


Python has 7 types of operators. Operators will ease your programming pains.
Operator are symbols that perform mathematical operations on variables or on values.

  • Arithmetic Operators
    + addition, - subtraction, * multiplication, / division, ** exponentiation, // floor, % modulus

  • Relational operators
    > greater than, < less than, == equality checker, <= less than/equal to, >= greater than/equal to, != not equal to

  • Assignment operators
    = assign, += add & assign, -= subtract & assign, *= multiply & assign, / divide & assign, **= exponentiation & assign, //= floor & assign % modulus & assign

  • Logical operators
    or- logical or, and- logical and, not logical not

  • Membership operators
    in, & not in

  • Identity operators
    is && is not

  • Bitwise operators
    They operate on values bit by bit.
    & Bitwise and, | Bitwise or, ^ Bitwise xor, ~ Bitwise 1’s complement, << Bitwise left shift, >> Bitwise right shift

Python Number data types

Python has three numeric data types: int, float, & complex.

  • int: an integer is a whole number that can be either positive or negative.
num = 100
code = -3255522
long = 35656222554887711 #can be of unlimited length
Enter fullscreen mode Exit fullscreen mode
  • float: "floating point number" is a number, positive or negative, containing one or more decimals.
num2 = 10.2
year = 12E4 #can also be scientific numbers with an "e" 
#to indicate the power of 10.
zeus = -35.59 
Enter fullscreen mode Exit fullscreen mode
  • complex: Complex numbers are written with a "j" as the imaginary part.
x = 3+5j
y = 5j
z = -5j
Enter fullscreen mode Exit fullscreen mode

Type conversion

You can convert one data type to another using built-in methods.

#int(), float(), complex()
c = 1
b = 1.23
a = 5j
float(c) #is 1.0
int(b) #is 1
complex(c) #is 1+0j
Enter fullscreen mode Exit fullscreen mode


Strings are sequences of characters and are immutable.
they are enclosed in 'single quotes' or "double quotes".

'this is a str'
"also this"
"""and this too"""
Enter fullscreen mode Exit fullscreen mode

You cannot start a string with a single quote and end it with a double quote (and vice versa).

'post\'s are personal'
#use a backslash to escape quotes inside the str 
#if the match the enclosing ones.
Enter fullscreen mode Exit fullscreen mode

be sure to checkout more on strings for more info.

Python Data Structures

Python data structures store collections of values. they are:

  • Lists
  • Tuples
  • Sets and frozensets
  • Dictionaries
  • Strings


Lists are ordered items and are mutable, means can be changed.
Lists hold any type of data type.
Tip: counting/indexing in python starts at 0

myList = [1, 'python', ['a nested List']] #defining a List
#List methods
myList[0] #prints 1
myList.append('name') #adds 'name' at the end of the list
len(myList) # prints 3, we appended 'name' ;)
Enter fullscreen mode Exit fullscreen mode


Tuples are collections of Python objects.
Tuples are similar to lists, but are immutable, cannot be changed.
Note: To create a single item tuple, you have to use a comma after the value. num = (5,)

#creating tuples
fruits = ('mango', 'apple', 'kiwi')
print(fruits[1]) #'apple'
for x in fruits:
Enter fullscreen mode Exit fullscreen mode


Dictionaries in Python are collections that are unordered, indexed and mutable.
They hold keys and values. Are just like real life dictionaries, consisting of words and their meanings.
Python dicts consist of key, value pairs.
This is a dictionaries:

animals={'dog': 'mammal', 'cat': 'mammal', 'snake': 'reptile', }

#indexing, can be accessed using keys
animals #prints 'dog': 'mammal', 'cat': 'mammal', 'snake': 'reptile'
animals['cat'] # 'mammal'

#get() method gets specified key
animals.get('dog') #'mammal'

#adding items
animals['frog'] = 'amphibian' #pushes frog to end of dict

#pop() removes specified item
pop('cat') #'mammal' removed and returned

#del keyword to delete dictionary items.
del animals['dog'] 

#clear() removes all the key values pairs from the dict
animals.clear() # {}

#changing values
animals['cat'] = 'just a cat'
Enter fullscreen mode Exit fullscreen mode

Read more dictionatries.


Sets in Python are a collection of unordered and unindexed Python objects.
Sets are mutable, iterable and they do not contain duplicate values.

#creating a set
tickets = {1, 2, 3, 4, 5}
items = {99, 100, 200, yes, no}

#creating empty sets
empty_set = set()

#accessing sets
a = {7, 6, 5, 4, 3, 4, 5, 6}
#>> {3, 4, 5, 6} #sets do not contain duplicate values

#deleting in sets
# discard(), remove(), pop() and clear().
a = {1, 2, 3, 4}
a.discard(3) # {1,2,4} remove() is similar do discard

print(a.pop()) #> 4

print(a.clear()) #> {}
Enter fullscreen mode Exit fullscreen mode

There is more to sets than just this.

Conditionals or Decision making

Python decision making.
Decisions are useful when you have conditions to execute after a certain action, like a game, or checking for even numbers etc.
Python has got you, we use if, if-else, if-elif, & nested conditions in situations like this.

#python if statement
num = 3
if(num % 2 == 0):

#if-else statement
if(12 % 2 == 0):

if( expression1 ):
elif (expression2 ) :
elif(expression3 ):
Enter fullscreen mode Exit fullscreen mode

You will encounter conditions everywhere throughout your coding journey. Be sure to read more.


Now we checkout python loops.
Loops are one of the most powerful and basic concepts in programming.
A loop can contain a set of statements that keeps on executing until a specific condition is reached.
Python has two types of loops: for loop & while loop

for loop

Used to items like list, tuple, set, dictionary, string or any other iterable objects.

for item in sequence:
    body of for loop
Enter fullscreen mode Exit fullscreen mode

for loop continues until the end is reached.

while loop

It executes a block of code until the specified condition becomes False.

counter = 0
while(counter < 10):
  counter = counter +1
#prints 1-10
Enter fullscreen mode Exit fullscreen mode

Controlling the flow with:

python lets you control the flow of execution in a certain way
break statement inside a loop is used to exit out of the loop.
continue statement is used to skip the next statements in the loop.

Loops are bit extensive than just this but i believe this guide gave you an idea of what python is.

That's it for now. Bless.
Be cool, Keep coding.

Discussion (0)