Open In App

Python 3 basics

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.11.0, we can simply call it Python3. Python 3.0 was released in 2008. and is interpreted language i.e it's not compiled and the interpreter will check the code line by line. This article can be used to learn the very basics of Python programming language .

We also have a complete Python 3 Tutorial designed to learn Python 3 at all levels, from beginners to advanced. This comprehensive tutorial takes you through the fundamental concepts of Python and gradually progresses to more advanced topics. Along with this you can also explore a online python self paced course for more feasibility.

Python 3 Basics

Python 3 is a popular high-level programming language used for a wide variety of applications. Here are some basics of Python 3 that you should know:

  1. Variables : In Python 3, variables are created by assigning a value to a name. For example, x = 5 creates a variable called x and assigns the value 5 to it.
  2. Input and output : In Python 3, you can use the input() function to get user input, and the print() function to output text to the console.
  3. Data types : Python 3 supports several built-in data types, including integers, floats, strings, booleans, lists, tuples, and dictionaries.
  4. Operators : Python 3 supports a variety of operators, including arithmetic operators (+, -, *, /), comparison operators (>, <, ==, !=), and logical operators (and, or, not).
  5. Control flow statements : Python 3 supports several control flow statements, including if-else statements, for loops, and while loops. These statements allow you to control the flow of execution in your code.
  6. Functions : In Python 3, functions are created using the def keyword. For example, def my_function(x): creates a function called my_function that takes one argument called x.
  7. Modules : Python 3 supports modules, which are collections of functions and variables that can be imported and used in other Python code. You can import modules using the import keyword.

Advantages of Python 3

  1. Python 3 has a simple syntax that is easy to learn and read, making it a good choice for beginners.
  2. Python 3 is a high-level language that has a large standard library and many third-party libraries available, making it a versatile language that can be used for a wide variety of applications.
  3. Python 3 supports multiple programming paradigms, including object-oriented, functional, and procedural programming.
  4. Python 3 is an interpreted language, meaning that it does not need to be compiled before running, making it easy to write and test code quickly.
  5. Python 3 has good support for data analysis and scientific computing, with libraries such as NumPy and Pandas.
  6. Easy to learn and user-friendly

Disadvantages of Python 3

  1. Python 3 can be slower than compiled languages such as C++ or Java, which may be a concern for applications that require high performance.
  2. Python 3 has a global interpreter lock (GIL), which can limit its ability to take advantage of multiple CPU cores.
  3. Python 3 may not be the best choice for low-level systems programming, as it does not offer the same level of control over hardware as other languages.
  4. Python 3 is not as popular in some fields as other languages, such as R for data analysis or C++ for game development, so it may not always be the best choice for specific applications.

Let's do the most popular 'HelloWorld' tradition and hence compare Python's Syntax with C, C++, and Java ( I have taken these 3 because they are the most famous and mostly used languages).

Python
# Python code to print the Hello, World!;
# nothing else to type...see how simple is the syntax.

print("Hello, World!")      

Output
Hello, World!

Note: Please note that Python for its scope doesn't depend on the braces ( { } ), instead it uses indentation for its scope. Let us start with our basics of Python where we will be covering the basics in some small sections. Just go through them and trust me you'll learn the basics of Python very easily.

Introduction and Setup

If you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as a Python's IDE to run the Python Scripts. It will look somehow this :

If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type 'python3' in terminal and you are ready to go. It will look like this :

Capture
The ” >>> ” represents the python shell and its ready to take python commands and code.

Variables and Data Structures

In other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.

Python Program to Illustrate a DataTypes

Python
# print the integer number
myNumber = 3	
print(myNumber)
 
#print the float number
myNumber2 = 4.5	
print(myNumber2)
 
#print the string
myNumber ="helloworld"
print(myNumber)

Output
3
4.5
helloworld

See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built-in Data Structures namely List , Dictionary , Tuple, and Set .

Python Program to Illustrate a List

List is the most basic Data Structure in Python. Python List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list.
append() function is used to add data to the list.

Python
# creates a list 
list1=["geeks","for","geeks",100,10.19,5j+10,]

#print the list
print("the list is: ",list1)
 
 #print first index data
print("the zeroth index value: ",list1[0])

#print range of list
print("the index of 0 to 4 datas in list: ",list1[0:4])

#print backward of list
print("the backward is list: ",list1[::-1])

Output
the list is:  ['geeks', 'for', 'geeks', 100, 10.19, (10+5j)]
the zeroth index value:  geeks
the index of 0 to 4 datas in list:  ['geeks', 'for', 'geeks', 100]
the backward is list:  [(10+5j), 10.19, 100, 'geeks', 'for', 'geeks']

Python program to illustrate a Dictionary

A Python dictionary is a data structure that stores the value in key:value pairs. Python dictionaries are essential for efficient data mapping and manipulation in programming.

Python
# creates a empty list
Dict = []

# putting integer values
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}

print(Dict)

#print the value in dict using keyword
print("the value of key is:", Dict[3])

Output
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
the value of key is: Geeks

Python program to illustrate a Tuple

A Python tuple is a collection of objects separated by commas. While it shares similarities with a Python list—such as indexing, nested objects, and support for repetition—the key distinction is that tuples are immutable, meaning their elements cannot be changed after creation, whereas lists are mutable and allow for modifications.

Python
# creates a tuple which is immutable
tup = ('Geeks', 'For', 'Geeks')

print(tup)

#print the value by index
print(tup[2])

Output
('Geeks', 'For', 'Geeks')
Geeks

Python Program to illustrate a Set

A Set in Python programming is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Python
# define a set and its elements
myset = set(["Geeks", "For", "Geeks"])

#as set doesn't have duplicate elements so, 1 geeks will not be printed
print(myset)

#union in set
myset= myset.union({"good","online","learning","platform"})
print(myset)

#intersection in set
set1={"geeks","online","platform"}
set2={"for","geeks","platform"}

new_set=set1.intersection(set2)
print(new_set)

Output
{'Geeks', 'For'}
{'Geeks', 'platform', 'good', 'For', 'online', 'learning'}
{'platform', 'geeks'}

Comments:

# is used for single line comment in Python
""" this is a comment """ is used for multi line comments

Input and Output in Python

In this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user.

Python
# Python program to illustrate
# getting input from user
name = input("Enter your name: ") 
 
# user entered the name 'harssh'
print("hello", name)

Output:

hello harsh

Python program to get Input from User

Python
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
 
 #Multiplicaion
num3 = num1 * num2
print("Product is: ", num3)

num3 = num1 * num2
print("Product is: ", num3)

#Addition
num4 = num1 + num2
print("Addition is: ", num4)

#Subtraction
num5 = num1 - num2
print("Subtraction is: ", num5)

#Division
num6 = num1 / num2
print("Division is: ", num6)


Output:

Enter num1: 8 Enter num2: 6 ('Product is: ', 48)

Python Conditional Statement

Condition in Python is made using the two keywords ‘if’ and ‘elif’(elseif) and else.

Python
# Python program to illustrate Conditional statement
 
num1 = 34
if(num1>12):
   print("Num1 is good")
elif(num1>35):
   print("Num2 is not gooooo....")
else:
   print("Num2 is great")

Output
Num1 is good

Python Functions

You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function.
Syntax:

def function-name(arguments):
#function body
Python
# Python program to illustrate functions
def hello():
    print("hello")
    print("hello again")


  # initiate the function by calling function name
hello()

# calling function
hello()

Output
hello
hello again
hello
hello again

Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.

Python
# Python program to illustrate 
# function with main
def getInteger():
    result = int(input("Enter integer: "))
    return result


def Main():
    print("Started")

    # calling the getInteger function and 
    # storing its returned value in the output variable
    output = getInteger()
    print(output)


# now we are required to tell Python 
# for 'Main' function existence
if __name__ == "__main__":
    Main()

Python Iteration (Looping)

As the name suggests it calls repeating things again and again. We will use the most popular ‘for and while’ loop here.

Python
# print the iteration in single line
print("For Loop:")
for step in range(5):
    print(step)
    
# a simple while loop
print("While Loop:")
step = 0
while(step < 5):
    print(step)
    step = step+1

Output
For Loop:
0
1
2
3
4
While Loop:
0
1
2
3
4

Python Modules

Python has a very rich module library that has several functions to do many tasks. ‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.

Python
# Python program to illustrate math module
import math
 
#factorial for given number
def fact(num):
  factorial=1
  for i in range(1,num+1):
    factorial*=i
  return factorial

num=5
#print factorial
print(fact(num))
     
     

Output
120

These are some of the basics of the Python programming language and I will be covering both the intermediate and advanced level Python topics in my upcoming articles.


Next Article
Article Tags :
Practice Tags :

Similar Reads