Open In App

Learn Python Basics

Last Updated : 17 Dec, 2025
Comments
Improve
Suggest changes
80 Likes
Like
Report

Python is a high-level programming language with a simple and readable syntax. It is commonly used for web development, data analysis, automation and machine learning. This article covers the basic concepts of Python to help beginners start coding.

To begin, install Python on your system from the official Python website.

First Python Program

Once installed, we can write and execute Python code using an Integrated Development Environment (IDE) like PyCharm, Vs Code (requires installing Python extension) etc.

Python
print("Hello Geeks, Welcome to Python Basics")

Output
Hello Geeks, Welcome to Python Basics

Explanation:print() is a built-in function that outputs text or variables to the console. In this case, it displays the string "Hello, Geeks! Welcome to Python Basics".

Comments in Python

Comments are lines in a program that are not executed by the interpreter. They are used to explain code and make it easier to read and understand.

Python
# This is a single-line comment

"""
This is a
multi-line comment
or docstring.
"""

Explanation:

  • #: Denotes a single-line comment.​
  • """ """ or ''' ''': Triple quotes are used for multi-line comments or docstrings.

Variables in Python

Variables are names that store data in memory. They are created when a value is assigned, with the name referencing that value. Python is dynamically typed, so no data type declaration is required. Rules for naming variables in Python are:

  • Must start with a letter (a–z, A–Z) or an underscore (_)
  • Cannot start with a number
  • Can contain only letters, numbers, and underscores
  • Variable names are case-sensitive (name, Name, and NAME are different)
  • Python keywords cannot be used as variable names
Python
# Integer assignment
a = 45

# Floating-point assignment
b = 1456.8

# String assignment
c = "Geek"

print(a)    
print(b) 
print(c) 

Output
45
1456.8
Geek

Data Types in Python

Data types define the kind of data a variable can hold and determine the operations that can be performed on it. In Python, every value is an object and each object belongs to a specific data type (class).

python_data_types-1
Data Types in Python

Example: The following example shows how the same variable can store values of different data types in Python.

Python
x = "Hello World" # string
x = 50  # integer
x = 60.5  # float
x = 3j  # complex
x = ["geeks", "for", "geeks"]  # list 
x = ("geeks", "for", "geeks")  # tuple
x = {"name": "Suraj", "age": 24} # dict
x = {"geeks", "for", "geeks"} # set
x = True  # bool
x = b"Geeks" # binary

Python Input/Output

Python provides simple built-in functions to take input from the user and display output on the screen.

1. Input: The input() function is used to take input from the user. By default, the value entered by the user is stored as a string.

Python
val = input("Enter your value: ")
print("You entered:", val)

Output

Enter your value: 11
You entered: 11

If you need the input in another data type (such as int or float), you must convert it explicitly.

Python
name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(type(name))
print(type(age))

Output

Enter your name: Jake
Enter your age: 12
<class 'str'>
<class 'int'>

Explanation:

  • input() always returns a string.
  • int() converts the input into an integer.
  • Use float() to convert input into a decimal value.

2. Output: The print() function is used to display values or messages.

Python
print "Hello, Geek!"

Output
Hello, Geek!

Python Operators

Operators in Python are symbols used to perform operations on values and variables, such as calculations, comparisons, and logical checks.

1. Arithmetic Operators: These are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. Types of arithmetic operators: +, -, *, /, //, %, **​. Precedence of Arithmetic Operators are as follows:

  1. P - Parentheses
  2. E - Exponentiation
  3. M - Multiplication (Multiplication and division have the same precedence)
  4. D - Division
  5. A - Addition (Addition and subtraction have the same precedence)
  6. S - Subtraction
Python
a = 9
b = 4
add = a + b 
sub = a - b 
mul = a * b 
mod = a % b 
exp = a ** b 

print(add) 
print(sub) 
print(mul) 
print(mod) 
print(exp) 

Output
13
5
36
1
6561

2. Comparison Operators: These are used to compare two values. They return a Boolean value either True or False depending on whether the comparison is correct.

Python
a = 10
b = 20

print(a == b)   # False, because 10 is not equal to 20
print(a != b)   # True, because 10 is not equal to 20
print(a > b)    # False, 10 is not greater than 20
print(a < b)    # True, 10 is less than 20
print(a >= b)   # False, 10 is not greater than or equal to 20
print(a <= b)   # True, 10 is less than or equal to 20

Output
False
True
False
True
False
True

Explanation:

  • Each print() checks a condition.
  • The result is either True or False depending on whether the comparison holds.
  • These operators are commonly used to control program flow in if statements, loops, etc.

3. Logical Operators: It perform Logical AND, Logical OR and Logical NOT operations. It is used to combine conditional statements. Types of logical operators are: AND, OR, NOT​.

Python
a = True
b = False
print(a and b) 
print(a or b) 
print(not a) 

Output
False
True
False

4. Bitwise Operators: This act on bits and perform bit-by-bit operations. These are used to operate on binary numbers. Types of bitwise operators are: &, |, ^, ~, <<, >>​

Python
a = 10
b = 4
print(a & b) 
print(a | b) 
print(~a) 
print(a ^ b) 
print(a >> 2) 
print(a << 2) 

Output
0
14
-11
14
2
40

5. Assignment Operators: These are used to assign values to the variables. Types of assignment operators: =, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, >>=, <<=​.

Python
a = 10
b = a 
print(b) 
b += a 
print(b) 
b -= a 
print(b) 
b *= a 
print(b) 
b <<= a 
print(b)

Output
10
20
10
100
102400

Python If Else

In Python, the if statement runs a block of code when a condition is True. If the condition is False, the else block runs. This helps programs make decisions based on conditions.

Example 1: This example checks whether the value of i is less than 15. If the condition is true, the if block runs; otherwise, the else block runs.

Python
i = 20
if (i < 15): 
    print("i is smaller than 15") 
    print("i'm in if Block") 
else: 
    print("i is greater than 15") 
    print("i'm in else Block") 
print("i'm not in if and not in else Block") 

Output
i is greater than 15
i'm in else Block
i'm not in if and not in else Block

Explanation:

  • The condition i < 15 is False, so the else block is executed.
  • The last print() runs no matter what because it's outside the if-else structure.

Example 2: This example checks multiple conditions for the value of i. Python evaluates each condition in order and executes the block where the condition becomes true.

Python
i = 20
if (i == 10): 
    print("i is 10") 
elif (i == 15): 
    print("i is 15") 
elif (i == 20): 
    print("i is 20") 
else: 
    print("i is not present") 

Output
i is 20

Explanation:

  • Python checks conditions from top to bottom.
  • Once a condition is True, it executes that block and skips the rest.
  • If none of the conditions match, it executes the else block.

Python Loops

1. For Loop: It is used to iterate over a sequence such as a list, string, or a range of numbers. It runs a block of code once for each item in the sequence. Below example uses range() to generate numbers from 0 to 9 with a step of 2 and prints each value.

Python
for i in range(0, 10, 2): 
    print(i) 

Output
0
2
4
6
8

Explanation: range(0, 10, 2) generates numbers: 0, 2, 4, 6, 8 and loop prints each number on a new line.

2. While Loop: It continues to execute as long as a condition is True. In below example, the condition for while will be True as long as the counter variable (count) is less than 3.

Python
count = 0
while (count < 3): 
    count = count + 1
    print("Hello Geek")

Output
Hello Geek
Hello Geek
Hello Geek

Explanation:

  • The loop runs 3 times because count goes from 0 - 1 - 2.
  • Once count becomes 3, the condition count < 3 becomes False and the loop stops.

Also see: Use of break, continue and pass in Python

Python Functions

Python Function is a block of reusable code that performs a specific task. Functions help make your code modular, readable, and easier to debug. There are two main types of functions in Python:

  1. Built-in functions like: print(), len(), type()
  2. User-defined functions created using the def keyword
frame_3255
User Defined Function

Example: This example shows a simple user-defined function that checks whether a number is even or odd using a conditional statement.

Python
def evenOdd(x):
    if x % 2 == 0:
        print("even")
    else:
        print("odd")

evenOdd(2)
evenOdd(3)

Output
even
odd

Explanation:

  • The function evenOdd(x) checks if x is divisible by 2.
  • If it is, it prints "even"; otherwise, "odd".

What's Next

After learning Python basics, you can move forward in these areas:

  1. Continuous Python Learning: Explore structured tutorials that cover Python from beginner to advanced levels.
  2. Advanced Python Concepts: Learn features like list comprehensions, lambda functions, and generators.
  3. Python Packages and Frameworks: Use libraries such as Django/Flask (web), Pandas/Matplotlib (data), TensorFlow/PyTorch (ML), and Selenium/BeautifulSoup (automation).
  4. Build Python Projects: Apply your knowledge by creating real-world Python projects.

Article Tags :

Explore