Python is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial intelligence, and more. In this article, we'll cover the foundational concepts of Python programming to help you get started.
Writing First Python Program
To begin coding in Python, we'll need to have Python installed on our system. You can download the latest version from the official Python website. Once installed, we can write and execute Python code using an Integrated Development Environment (IDE) like PyCharm, Vs Code (requires installing Python extension), or even a simple text editor.
Python
print("Hello Geeks, Welcome to Python Basics")
OutputHello 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 in Python are the lines in the code that are ignored by the interpreter during the execution of the program. Also, Comments enhance the readability of the code and help the programmers to understand the code very carefully.
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
Python Variable is a container that store values. Python is not “statically typed”. An Example of a Variable in Python is a representational name that serves as a pointer to an object. Once an object is assigned to a variable, it can be referred to by that name.
Rules for Naming Variables
- Must start with a letter (a-z, A-Z) or an underscore (_).
- Cannot start with a number.
- Can only contain alphanumeric characters and underscores.
- Case-sensitive (name, Name, and NAME are different variables).
- The reserved words(keywords) in Python cannot be used to name the variable in Python.
Example:
Python
# Integer assignment
age = 45
# Floating-point assignment
salary = 1456.8
# String assignment
name = "Geek"
print(age)
print(salary)
print(name)
Data Types in Python
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.

Example: This code assigns variable ‘x’ different values of various 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 provides simple functions for input and output operations.
Input
The input() function allows user input, example:
Python
val = input("Enter your value: ")
print("You entered:", val)
Output:
Enter your value: 11
You entered: 11
The input() function in Python always returns data as a string, regardless of what the user enters. If we want to store the input as another data type (like int, float, etc.), we need to explicitly convert (typecast) it.
Example:
Python
name = input("Enter your name: ")
print(type(name))
age = int(input("Enter your age: "))
print(type(age))
Output:
Enter your name: Geeks
<class 'str'>
Enter your age: 8
<class 'int'>
Explanation:
- name stores the input as a string (default behavior of input()).
- age stores the input as an integer using int() for typecasting.
Python
# Python program show input and Output
val = input("Enter your value: ")
print(val)
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Arithmetic Operators
Python Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. Types of arithmetic operators: +, -, *, /, //, %, **
Precedence of Arithmetic Operators
The precedence of Arithmetic Operators in Python is as follows:
- P – Parentheses
- E – Exponentiation
- M – Multiplication (Multiplication and division have the same precedence)
- D – Division
- A – Addition (Addition and subtraction have the same precedence)
- S – Subtraction
Example:
Python
a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
mod = a % b
E = a ** b
print(add)
print(sub)
print(mul)
print(mod)
print(E)
Comparison Operators
Comparison operators are used to compare two values. They return a Boolean value — either True or False — depending on whether the comparison is correct. These operators are often used in conditional statements like if, while, and loops.
Example:
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
OutputFalse
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.
Logical Operators
Python Logical operators perform Logical AND , Logical OR , and Logical NOT operations. It is used to combine conditional statements. Types of logicsl operators: and, or, not.
Python
a = True
b = False
print(a and b)
print(a or b)
print(not a)
Bitwise Operators
Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers. Types of bitwise operators: &, |, ^, ~, <<, >>
Python
a = 10
b = 4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
Assignment Operators
Python Assignment operators 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)
Output10
20
10
100
102400
Python If Else
In Python, the if statement is used to run a block of code only when a specific condition is true. If the condition is false and you want to run a different block of code, you can use the else statement. This allows your program to make decisions and respond differently based on conditions.
Example 1: Python IF-Else
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")
Outputi 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: Python if-elif-else ladder
Sometimes, weneed to check multiple conditions. In such cases, Python provides the if-elif-else structure.
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")
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
For Loop
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple, List, Set, or Dictionary. Here, we will see a "for" loop in conjunction with the range() function to generate a sequence of numbers starting from 0, up to (but not including) 10, and with a step size of 2. For each number in the sequence, the loop prints its value using the print() function.
Python
for i in range(0, 10, 2):
print(i)
Explanation:
- range(0, 10, 2) generates numbers: 0, 2, 4, 6, 8.
- The loop prints each number on a new line.
While Loop
A while loop continues to execute as long as a condition is True. In this 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")
OutputHello 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:
- Built-in functions like print(), len(), type()
- User-defined functions created using the def keyword

Example: User-Defined Function to Check Even/Odd
Python
def evenOdd(x):
if x % 2 == 0:
print("even")
else:
print("odd")
evenOdd(2)
evenOdd(3)
Explanation:
- The function evenOdd(x) checks if x is divisible by 2.
- If it is, it prints "even"; otherwise, "odd".
What's Next
After understanding the Python Basics, there are several paths you can explore to further enhance your skills and delve deeper into the language:
- Continuous Python Learning : This article offers a well-organized, in-depth tutorial on Python, guiding learners from the basics to more advanced topics.
- Advanced Python Concepts : This article covers some advance Python concepts that distinguishes Python from any other language such as list comprehenison, lambda function, etc.
- Python Packages and Frameworks : Python has a rich ecosystem of libraries and frameworks for a wide range of tasks such as web development with frameworks like Django or Flask, data analysis and visualization with libraries like Pandas and Matplotlib, machine learning and artificial intelligence with TensorFlow or PyTorch, or automation with libraries like Selenium or BeautifulSoup This article explores them in detail.
- Build Python Projects : Learn how to build real-world applications using Python. This section includes a variety of projects to help you apply your skills and create meaningful solutions.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read