Python For Beginners: Part-1

Aashiq Reza
Data For Tomorrow
Published in
9 min readNov 24, 2020

Basic Idea

Introduction:

Python is an interpreted, high-level, and general-purpose programming language. Python’s design philosophy emphasizes code readability with it’s notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large scale projects. (wikipedia)

Photo by Emile Perron on Unsplash

Python is the most used programming language in recent years all around the world. Of course for some reasons. I’m not going deep, but I’d like to show you some reasons for using python:

  1. Code is easy to read, use, and maintain.
  2. Multiple programming paradigms supported.
  3. Compatible with major system platforms.
  4. User-friendly standard libraries.
  5. Availability of open sources and different tools.
  6. Large user community.
  7. Application in different fields.

Now let's get started with some basic python programming.(Different platforms can be used for python programming but I prefer Jupyter Notebook)

Write down your first line of python:

Let’s start by simply writing down some lines. Using print() function helps you to do so. the print() function actually prints whatever the input is. Take a look at the code:

print("Hello Python Programmer")

then the output will be:

Hello Python Programmer

Congratulation! You ran your first python code.

Basic Data Types and Operations:

Now write down these two lines given below:

my_var = 7
print(my_var)

Guess what will be the answer? You have assigned the value “7” to a variable called “my_var” and passed “my_var” through print() function. So the print() should show “7” on the answer. And it is 7.

7

Now we’d like to check what is the type of this variable. To do so, we can use type() function.

type(my_var)

And for this variable, the answer will by int. (Integer)

int

Let’s assign another value to a variable and find out the type.

my_float = 3.66
print(my_float)
type(my_float)

And the output will look like:

3.66float

So fractional numbers are float type in python. If you have done c/cpp, I expect that you are familiar with float and double terms.

We have been working with numbers. But what about character input? What if I try to assign some words or text to a variable? Let’s try something out:

my_string = "Hello"
print(my_string)

Here I have assigned a word to a variable. Be careful that I used quotation marks to assign that word. And in the second line, I have passed the variable through print() function. And the output will look like:

>>>Hello

Awesome! print() function really prints everything you want. But what will be the type of this variable? To check this one out:

type(my_string)

And the output will be:

>>>str

str means this is a string type variable. String takes care of characters in programming languages.

Now let’s do some arithmetic. Let’s add two variables:

print(my_var + my_float)

This will print the sum of the number assigned to the variables:

>>>10.66

Now try out something else. Let’s add a number with a string.

print(my_float + my_string)

What should be the output? Will it return something? No, this is gonna show you some errors. Because you can’t add two different types of data.

Error for adding float and str

But what if you add two strings? Let’s declare another string and print the sum of the two strings we have:

my_string2 = " Python Programmer"
print(my_string + my_string2)

Will it show any error? No. This will simply add these two strings. Which is called “concatenating strings”.

>>>Hello Python Programmer

Now imagine you need to print a number as well as a string. How can you do that? You can simply write down the code below:

print(my_var, my_string)

And the output will look like:

>>>7 Hello

Try out something more like this:

print(my_var, my_float)
print(my_string, my_string2)

Which will show you:

>>>7 3.66
>>>Hello Python Programmer

Now imagine that you need to store more than one number in a list. how can you do that?

my_list = [1,2,3,4]
print(my_list)

This code above will solve your problem. Now look at your output:

type(my_list)
>>> list

Aw nice! Another type this is! Yes, this is called a list. A list can contain more than one information.

Okay, now I’d like to show you the 4th element of the list.

print(my_list[4])

NOTE: [4] is used to print only the 4th element. Okay, look how does the output look like:

Indexing Error

Oops! It’s an error. 4 inside the third bracket is called index which indicates the position of an object inside a list. Let’s try something else:

print(my_list[3])
>>> 4

Here I get my desired output. But it’s the 4th element, right? Now write down something else:

print(my_list[0])
>>> 1

I think it’s clear now. Indexing starts from 0 to n-1. Where n is the number of elements.

Now let’s see how to handle a list containing characters:

names = ["Ronaldo", "Casillas", "Messi"]
# only Ronaldo loves python. Unlike Messi and Suarez, he's more than a footballer.
print("Welcome to python programming,", names[0])

What will be the output? Will Ronaldo’s name be shown? Take a look:

>>>Welcome to python programming, Ronaldo

Yeah. So indexing does the same for characters are well. And the type of “names” will still be a list.

type(names)
>>> list

Now let’s have a little fun. Python works as a calculator as well. Let’s do some basic arithmetic operations:

print(a+b) # sum 
print(a-d) # substraction
print(a*b-c/d) # multiplication, substraction, division
print(a+b+c-d+a*b)
squared = b**a # squared
floor = b // a # floor division
print(floor)

Take a look at the output:

Arithmetic Operations

Now a little bit of fun. I’d like to print hello. But ten times. Can you do this for me? I’m not sure, but you might think of writing down print(“hello”) ten times. But it’s really time killing. What if you can do this in one go? Let’s try once:

ten_hello = "Hello " * 10
print(ten_hello)
>>> Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello

That’s great, isn’t it?

Now let’s do some operations on lists. Try the codes below:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = list1 + list2
print(list3) # concatenate two lists
print(list1*2) # printing same list twice

As far as we have learned, list3 should add up both the lists, not their elements, and the last line should print the same list twice. Right? Take a look!

>>>[1, 2, 3, 4, 5, 6, 7, 8]
>>>[1, 2, 3, 4, 1, 2, 3, 4]

Now let’s find out the length of each list. We can use len() to do so. Then store this in respective variables:

l1 = len(list1)
l2 = len(list2)
l3 = len(list3)

I can use print(l1) to print the length of list1 easily. But we have done this kind of thing again and again in this article. Let’s try something new. I’d like to print the lengths with some words. How to do this? Check the line below:

print("Length of list1 is %(l1)d and length of list3 is %(l3)d ." %{"l1":l1, "l3":l3})
>>> Length of list1 is 4 and length of list3 is 8.

Now you can write down numbers within characters using print().

Let’s do this kind of thing with a list containing characters.

print("three of my favourite footballers are:", "," .join(names))
>>> three of my favourite footballers are: Ronaldo,Casillas,Messi

That’s perfect, right?

Now check out the length of names using len():

length1 = len(names)
print(length1) # names contains three different elements
>>> 3

As “names” contains three names separated by “comma(,)”, the length is 3.

Write down the code below:

string = "Hello Ronaldo"
length2 = len(string)
print(length2)
print(string.index("R"))

What should be the length now? And what is string.index()?

Here the length should be the number of characters and the command string.index(“R”) will show you at which position you will find “R”.

>>> 13
>>> 6

Is it right? You can check it for yourself.

Let’s try some fancy things you might need. With a string of course.

print(string[::-1]) # reverse string
print(string.lower()) # all lowe-case
print(string.upper()) # all upper-case
split = string.split(" ") # splits string
print(split)
try1 = 'string' # string can be represented with '' also
print(try1)

Really simple commands, right? Take a look:

String operations

A lot of work done with string. Now imagine you need to compare some values. Finding if two values are equal or not. This type of logical comparison can be done very simply in python.

a = 2
b = 3
c = 4
d = 2
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 4]
print(a == b) # true if a is equal to b, false otherwise
print(a == d) # true if a is equal to b, false otherwise
print(a != 4) # true if a is not equal to 4, false otherwise
print(a > c) # true if a is greater than c, false otherwise
print(b < c) # true if b is less than c, false otherwise
print(a < b and c > 4) # true if a is greater than b and c is less than 4, false otherwise
print(l1 == l2) # true if l1 is equal to l2, false otherwise
print(c in l2) # true if the element c is in l2, false otherwise
print(not(c in l2)) # true if the element c not in l2, false otherwise

A lot of writings! Now take a look at the output:

Logical Operations

It’s all right.

Conditional Statement:

We have done enough for basic operations. In the conditional statement and loops section, we will try to use some of the basic ideas.

In python, unlike other languages, conditional statements are written in if…else form.

if (condition):
....
staement if the condition is true
....
else:
....
statement if the condition is false
....

Let’s do an example:

if a == b: # a = 2 and b = 3
print("TRUE")
else:
print("FALSE")

What should this one print? False? Yes. The output will be false as a is not equal to b.

>>> FALSE

We can impose more than one condition in one go. In that case, the if…else structure will obtain a few changes:

if (condtion1):
....
statement1 if condition1 is true
....
if (condition2):
....
statement2 if condition2 is true
....
...
...
...

So, in this case, we imposed quite a lot of conditions, as much as we need actually, to solve our problem. Let’s do with a simple example:

if a < 0:
print("Negative")
if a > 0:
print("Positive")
if a == 0:
print("Zero")
>>> Positive

In this case, a is equal to 2. So a is positive is correct.

Loops:

Suppose you want to calculate the sum of all numbers given in a list, you have to simply add up those numbers. To do this, you need to carry out the sum operation again and again. But for a long list containing so many numbers, it’s hard to calculate like this. We can use loops to solve this kind of problem. Firstly take a look at the structure of for loop on python:

for var in iterable:
....
statement
....

Now let’s solve the problem:

l1 = [1, 2, 3, 5, 8, 11]
total = 0
for item in l1:
total = total + item
print(total)
>>> 30

Correct! 30 is the sum of the numbers in the list.

You must have heard about while loop. Take a look at its structure first:

while condition:
....
statement
....

Let’s solve the problem above using a while loop.

l1 = [1, 2, 3, 5, 8, 11]
total = 0
i = 0
while i < len(l1):
total = total + l1[i]
i += 1
print(total)
>>> 30

It gives the same result. Great!

Check out two more problems:

  1. Using for loops
a = 3
fact = 1
for i in range(1,a+1): # 1:a would take 1 and 2 only. we need to get up to 3
fact = fact*i
print(fact)

2. Using while loop

i = 0
while(i < 5):
print(i)
i += 1
else:
print("Exceeded maximum iteration. %d" %(i))
for i in range(1, 10):
if(i % 5 == 0):
break
print(i)
else:
print("Loop is terminated before this as condition fails")

I won’t show the result for these two.

That’s enough for the first part of “Python for Beginners”. We’ll focus on functions, dictionaries, and packages in the next part.

--

--

Aashiq Reza
Data For Tomorrow

Data Science, ML, Image processing. Good hands in R, MATLAB, Python, SPSS, C/Cpp. Always free to connect : https://www.linkedin.com/in/aashiq-reza-2030b516a/