Basic Python Start to End

Ajay Mane
10 min readSep 11, 2021

--

Within 10 Min

Python Programming language is one of the top-most programming languages in the world and is famous among new developers because of its simplicity and ease of understanding. I know you are interested in learning Python but are confused about where to start and how to get your hands on it.

Well, in this article, you will learn everything about Python from scratch. We will learn its syntax, features, and statements. We will see multiple examples to understand each concept and syntax.

Installation of Python

Visit the Python Site to download the latest version of Python. To check whether Python is successfully installed or not, use the following command on your command prompt or terminal.

python --version

Python Syntax

The syntax of Python is different in its different versions. Currently, we have Python 2 and Python 3. Both have a parenthesis difference.

Python 2 has the below syntax of printing anything on the console screen.

#python 2
print "Hello world"

Python 3 has the below syntax of printing anything on the console screen.

#python 3
print("Hello world")

Python has always use indentation to indicate the code of the block. Remember one thing — Python is strict about the indentation if you miss the indentation or add more spaces this will not work or will throw an error.

if 3 > 4:    
print("hello world") # correct indentation
if 3 > 6:
print("hello world") #this will throw indentation error

In Python, variables are declared without datatype or any keyword like var or val.

x = 4
print(x) # 4

Python Basics

Python basics section will cover the input and output functions, variables declaration and we will take a look at some operators. Let learn one by one each.

Input/output

Python had a Print function to output any variable, string, or data into the console screen. To use this function type keyword print with parenthesis and inside the parenthesis type data to print.

x = 5
print(x) # 5
print("Python") # Python

You can output multiple data in print function like the following code example.

x1 = 4
x2 = 7
print(x1, x2) # 4 7

To take Input in Python we had a input() statement that takes a string argument like a message saying what to do. Syntax of input is input(“Enter your Data”).

#taking input from useruser
input = input("Enter something: ")

Variables

Variables are usually called storage areas for your data. In programming, you need something to store and use that stored data in the future in any area of your code.

As we saw in the syntax of Python we declare variables without any keyword or datatype at the start but still, Python has some rules to declare variables. Check that out in the following code example.

name = "Haider"  #string 
variablenumber = 4 #integer variable
44num = 5 #wrong way
$num234 = 6 #wrong way

Operators

Python had several operators and we will discuss one by one each and understand them with examples.

The assignment operator is the operator which is denoted by keyword = and can be used to assign any value to a variable in Python.

value = 56
# sign "=" is the assignment operator and its assign 56 to variable value.

Arithmetic Operator is the operators which are used for mathematical calculation like addition, subtraction and multiplication.

#Airthmetic Operators
3 + 4
3 - 6
5 / 5
8 * 6
7 ** 2
9 % 2
4 // 8

Logical Operator is the operators which include logic signs like and, or, not.

#logical Operators
if 3 == 2 and 3 == 9:
print("Yes")
elif 3 ==2 or 5 == 5:
print("Yes")

Comparison Operator is the operators which are used to the comparison between data.

#Comparison Operators
3 > 4
5 < 5
8 == 4
8 <= 12
8 >= 8
9 != 9

Well, these are the essential operators that you should know you will need in your Python programming career.

Data Type in Python

Python had 4 data types and we will discuss each of them and understand them with an example.

A string data type is a text form-data type that can hold numbers, alphabets, and special characters, and to write that type you need single or double quotes around the data.

An integer data type is a number form-data type without any decimal numbers but it can be negative or positive numbers. You can say it’s a whole number data type.

a float data type is a decimal form of data type it only contains the number with decimal points.

A bool data type is a result type data type that is base on true and false.

string = "Hello world"  #declare string with double qoutes around the data
integer = 45 #whole number values
floating = 56.5 #decimal point values
boolean = True #True/False

Loops

Python had two loops which are For and while loop and both are easy to use. Let us understand each by example code and we will see how each of them works.

For loop

for n in range(5):
print(n)
#output
#1
#2
#3
#4
#5

For loop in Python is used to iterate a sequence of objects (list, tuple, string) and any other iterable object. Let understand loop with a simple example, I will try to print number from 1–5 using for loop with a single line code.

The n is a variable that will store the value we got from the range() because for loop is iterating the range until 5. Below I mention some key points to get a better understanding.

  1. For loop started a number 1 is iterated from range()
  2. Number 1 is store in a variable n
  3. After every iteration loop body is executed
  4. n the variable is printed on the console screen
  5. 1 is not equal to 5 the process will repeat again until we got 5

While Loop

i=1
while i <= 10:
print(i)
i=i + 1

While loop, on the other hand, is the same as the for loop but its syntax is different. Unlike for loop while loop uses another variable to make their inner condition.

Let take a simple example we will print 1 to 10 numbers using a while loop. Below I made the example code for you.

These simple 4 lines of code will print 1 to 10 numbers on your console screen. While loop is the most usable loop in programming because of its controllable and easy to use.

Condition Statements ( IF-ELSE)

Condition statements are the most important part of the programming, it involves the condition that makes your program decide whether a thing is true or false. Let take a simple example.

Is 2 is greater than 3?. Well your answer will be false right 2 is lesser than 3 but how the computer will decide. In that case, we use conditional statements which involve IF and ELSE. In this section, we had three conditional Statements IF, ELIF and ELSE.

IF statement

IF statement is simply checking a condition is true or false. Below I made an example code for you in which we are checking if 2 > 3 or not.

if 2 > 3:  
print("Yes") # no output will shown
if 3 > 1:
print("Yes") # Yes will be shown on screen

Below are the points to explain this code.

  1. If the condition is checking is 2 > 3 if it is true then run the condition body print(“Yes”) otherwise, nothing will be printed.
  2. The second code is checking 3 > 1 and that is true so you will get a Yes printed on screen
  3. Well if the Condition is not satisfied the If condition body will never execute.

ELIF statement

Elif’s statement is used to expand our condition. When IF condition is failed we can shift our code to Elif condition in that case we can check another condition.

Take an example if 3 > 5 then print Yes otherwise, check Elif's condition which will check if 3 > 2then print Yes. Let see this in the code form.

if 3 > 5:  
print("Yes") # no output code will move to elif condition
elif 3 > 2:
print("Yes") # output Yes

Below I mention the key points to remember.

  1. When the IF condition is false then Elif condition will be check
  2. When the IF condition is true then Elif condition will not check

ELSE statement

Else statement is a simple final condition for your conditional statements. When IF and ELIF condition is false then we had final condition name ELSE condition.

if 3 > 5:
print("Yes")
elif 3 > 6:
print("Yes")
else:
print("3 is not greater")

Multiple ELIF

You can use multiple Elif in a conditional statement but the only striction is you can’t use Elif alone they always must be IF before First ELIF. Check the below code for an example of how you can use multiple Elif.

if 3 > 4:
print("Yes")
elif 3 > 7:
print("Yes")
elif 3 > 8:
print("Yes")
else:
print("No")

Functions

A Function is a block of reusable code which executes whenever you call it. The function is very useful in programming because of its reusable ability.
Python had some built-in functions one of them we are using is print() a function in which we pass data as an argument and they output it on screen.

Take an example you made a function for adding 2 numbers and you always call that function whenever you need to add two numbers. You don’t need to write adding formula again you just create a function add the formula and reuse that function whenever you want.

Syntax of function

To implement the function in Python we use a keyword def and after this, we name our function. Next, we had parenthesis and within the parenthesis, you can pass the arguments or leave it blank.

def myfunc():
pass

Concept of Functions

I hope you understand what is the function. Now we try to understand how to implement the function inside our code.

def Sum(num1, num2):
result = num1 + num2
print(result)
Sum(1, 2) # 3
Sum(4, 6) # 10

Below I mention key points to explain the upper code example.

  1. we had made a function name Sum and pass two arguments
  2. Inside the function body, we made a formula we can add both arguments print it out on screen
  3. I call the Sum() function and pass 1 and 2 in it. When the Original Sum() function received 1 and 2 it will store them in num1 and num2.
  4. I call again Sum() function and pass 4 and 6. It performs the same steps again to add those two numbers which we pass as an argument.

Return keyword

The example code I shown is printing the value within the function what happens if we need to get the result back from the function. In that case, we use the return keyword so they return the process data back to where the function is called.

def Sum(num1, num2):
result = num1 + num2
return result
print(Sum(1, 2)) # 3
print(Sum(4, 6)) # 10

Data Structures

Data Structures are used to store and access a large amount of data. It's just likes the variable but it’s a collection of variables. Python has many data structures most of which are mutable and immutable.

Diff in Mutable and Immutable

If the value of the object can be changed so that calls a mutable object and if the value of the object can’t be the change that calls an immutable object.

Types of Data Structures

Below are the types of common data structures present in Python.

  1. list
  2. Dictionary
  3. Tuple
  4. Set

List

The list is a mutable data structure in Python. You can add or remove data from the data structure. The list is mostly used in a data structure in Python because it is to use and it is mutable. Syntax of List is denoted with Square brackets [] and to store multiple data we separate them by comma .

mylist = [1, 2, 3, 4, 5]mylist2 = ["Haider", "Jeff", "Rowdy"] mylist3 = ["Haider", "45", 99, True]

To add the new data to the list we used a method append() and pass the new value in its parenthesis check the code example below to know how it works.

mylist = [1, 2, 3, 4, 5]
mylist.append(6)
print(mylist) # 1 2 3 4 5 6

Dictionary

Dictionary data structure is different from other data structures because it works on the key-value concepts. We assign every value in the dictionary a key that denotes its identity number. Below is the example code to understand it better.

dict1 = { 1: "Python", 2: "JavaScript", 3: "C++"} dict2 = { "first": "Apple", "Second": "Mango", "Third": "Pineapple" }

To add new key-value pair in the dictionary we had to use the method update().

dict1 = { 1: "Python", 2: "JavaScript", 3: "C++"}
dict1.update({4 : "Java"})

Tuple

A tuple is used to store multiple data into one variable it is an immutable data structure and stores ordered collection of data. Syntax of tuple is very simple. Unlike the list, It is represented with oval brackets ().

tuple = (1, 2, 4, 5, 7)
print(tuple) # 1, 2, 4, 5, 7
tuple2 = ("Python", "JavaScript")
print(tuple2) # Python, JavaScript

Set

Set is used to store multiple data in a single variable like other data structures. Unlike tuple and list, Set is represented with curly brackets {}. Set is immutable which means it is unchangeable. Duplicates values are not allowed in the set. If you Try to put duplicate data, Set will remove it automatically.

set = {1, 3, 5, 6}
print(set) # 1, 3, 5, 6
set2 = {"First", "Second", "Third", "First", "Second"}
print(set2) # First, Second, Third

Helpful Resources for Python

Below I mention some helpful resources for you that will help you to sharpen your Python skills.

I hope these websites will be very helpful in your python career and remember to keep practicing what you learn. Try to solve a different problem every day.

Final Thoughts and Tips

I’m very glad you had reached to end of this article and I hope you Something from this 10-minute article. I tried my best to give you a complete programming concept of Python. Now you can use this concept in any programming language. So What next?

You are ready to dive into python beginner’s projects and after you sharp your python skills you should move to Advanced Project and keep Practicing the new things. Till Then Happy Codding!

You can check out my other articles on Python to sharp your skills. And Don’t forget to Clap for this article 👏.

--

--

Ajay Mane
Ajay Mane

No responses yet