Fundamental Python programs that you need to know

Farhaan Nazirkhan
4 min readOct 3, 2022

--

Python is the world’s fastest growing programming language. Because of its high-level, interpreted, and object-oriented architecture, it is suitable for all types of software solutions. It has a simple syntax that is similar to natural language, making it easier to read and understand.

You can check the step-by-step guide for installing Python in Visual Studio Code from here or you can download PyCharm, an IDE for python from here.

Below are programs describing the input, output, assignment, loops, arrays, and other functionalities of Python 3. You can run these programs on your side to better understand how they function.

I) Program to display a message

print("Hello World!")

Output:

Hello World!

II) Program assigning values to variables

x=5 
#since integer 5 is assigned to x, then x becomes an integer variable, i.e., the datatype of x is now integer
outstring="Hello world!"
#since a text is assigned to outstring, then outstring becomes a string variable, i.e., the datatype of outstring is now string

III) Program performing addition of two numbers

x=5 
#value 5 is assigned to x
y=4sum= x+y
#adds the values of x and y and assigns the result to sum
print("The sum of", x, "and", y, "is", sum)
#prints the sum of x and y

Output:

The sum of 5 and 4 is 9

IV) Program asking input from the user

print("Enter your name: ", end="") 
#prompts the user to enter their name
name=input()
#reads the user's input and assigns it to the variable name

ANOTHER WAY

name=input("Enter your name: ") 
#prompts the user to enter his/her name

Terminal:

Please enter your name: Farhaan

V) Program with conditional statement (if…else)

num=input("Enter a number: ") 
#Assume only real numbers can be entered
if(int(num)>0):
#if the condition evaluates to true then positive is displayed
print("Positive")
else:
#if the condition evaluates to false then negative is displayed
print("Negative")

Output:

Enter a number: 5
Positive

VI) Program with a while loop

num = 1while num < 5: #while loop  print(num) #prints the value of num  num+= 1 #incrementing the value of num by 1

Output:

1
2
3
4

VII) Program with a for loop

x = ["1", "2", "3"] # List of stringsfor i in x: # Loop through the list   print(i)else:   print("No more data") # Print this when the loop is done

Output:

1
2
3
No more data

VIII) Program performing Iteration

text="Hello World!"
for i in text: #for each character in text
print (i, end="") #print the character

Output:

Hello World!

IX) Program with a for loop (in range)

sum=0
for i in range(10):
#0 to 9
sum= sum + 1
print(sum)

Output:

10

X) Arrays in Python

Python does not have built-in support for arrays but it has a data type called a list which is functionally very similar to arrays from C/C++.

Important methods for Array/List in Python

append()
clear()
copy()
count()
extend()
index()
insert()
pop()
remove()
reverse()
sort()

Below are examples of the uses of some of the methods.

array =["red", "blue", "yellow", "green"] #initial array

a) append()

print("The initial array: ",array)
array.append("purple") #add purple to the end of the array
print("Final array: ",array)

Output:

The initial array:  ['red', 'blue', 'yellow', 'green']
Final array: ['red', 'blue', 'yellow', 'green', 'purple']

b) remove()

print("The initial array: ",array)
array.remove("blue")
#remove blue from the array
print("Final array: ",array)

Output:

The initial array:  ['red', 'blue', 'yellow', 'green', 'purple']
Final array: ['red', 'yellow', 'green', 'purple']

c) pop()

print("The initial array: ",array)
array.pop(2)
#remove the element at index 2
print("Final array: ",array)

Output:

The initial array:  ['red', 'yellow', 'green', 'purple']
Final array: ['red', 'yellow', 'purple']

d) insert()

print("The initial array: ",array)
array.insert(0, "orange")
#insert orange at the beginning of the array(overwritting existing element)
print("Final array: ",array)

Output:

The initial array:  ['red', 'yellow', 'purple']
Final array: ['orange', 'red', 'yellow', 'purple']

e) sort() ascending

print("The initial array: ",array)
array.sort(reverse=False)
#sort the array in ascending order
print("Final array: ",array)

Output:

The initial array:  ['orange', 'red', 'yellow', 'purple']
Final array: ['orange', 'purple', 'red', 'yellow']

f) sort() descending

print("The initial array: ",array)
array.sort(reverse=True)
#sort the array in descending order
print("Final array: ",array)

Output:

The initial array:  ['orange', 'purple', 'red', 'yellow']
Final array: ['yellow', 'red', 'purple', 'orange']

Thank you so much for taking the time to read my article. Do not forget to follow me so that you do not miss out any future articles. You may also check out my portfolio from here.

--

--

Farhaan Nazirkhan

A proactive Software Engineering student with a passion for computer programming and Information Technology.