Comprehensive documentation for popular programming languages
Python is a high-level, interpreted, general-purpose programming language. It was created by Guido van Rossum in 1989 and first released in 1991.
Python is known for being simple, readable, and powerful. Interestingly, its name does not come from a snake, but from a British comedy show β Monty Pythonβs Flying Circus.
Python is widely used for:
An Interpreter is a special program that executes Python code line by line. Unlike a compiler, which translates the entire code at once, the interpreter runs the code immediately, making Python easier to debug and test.
CPython
(default Python interpreter), PyPy
, Jython
.Example:
If you write print("Hello")
, the interpreter immediately executes and shows:
Hello
An IDE is a software application that provides a complete environment for coding. It combines multiple tools into one place, making programming easier and faster.
Examples of IDEs for Python:
PyCharm
, VS Code
, Spyder
, IDLE
(comes with Python).
Interpreter | IDE |
---|---|
Executes Python code line by line. | Provides tools for writing, editing, running, and debugging code. |
Focuses only on running the code. | Helps in the entire development process. |
Example: CPython, PyPy | Example: PyCharm, VS Code |
print()
Function in Python
The print()
function is one of the most commonly used functions in Python.
It is used to display output on the screen.
print(object(s), sep=' ', end='\n', file=sys.stdout)
\n
).print("Hello, Students!")
# Output: Hello, Students!
print("Python", "is", "fun", sep="-")
# Output: Python-is-fun
print("Hello", end=" ")
print("World")
# Output: Hello World
A command in Python is an instruction given to the computer to perform a specific task.
These commands can be written in a Python file (.py
) or directly in the Python shell (IDLE/terminal).
print()
, input()
+, -, *, /, %
if
, else
, elif
for
, while
exit()
or quit()
# Input/Output
name = input("Enter your name: ")
print("Welcome", name)
# Conditional Command
x = 10
if x > 5:
print("x is greater than 5")
# Looping Command
for i in range(3):
print("Hello")
# Output:
# Hello
# Hello
# Hello
# Program 1
print("Hello, Students!")
This is the simplest Python program.
The print()
function is used to display output on the screen.
In this program, it prints the text: Hello, Students!
print()
β It is a built-in Python function used to show output."Hello, Students!"
β This is a string written inside double quotes ("").The output of this program will be: Hello, Students!
# Program 2: Addition of Two Numbers
a = 10
b = 20
sum = a + b
print("The sum is:", sum)
This program performs the addition of two numbers in Python.
It takes two numbers a = 10 and b = 20, adds them, and stores the result in the variable sum.
Finally, the print()
function displays the result.
a = 10
β Variable a is assigned the value 10.b = 20
β Variable b is assigned the value 20.sum = a + b
β Adds the values of a and b, then stores the result in sum.print("The sum is:", sum)
β Prints the message along with the result.The output of this program will be: The sum is: 30
A variable in Python is a name given to a memory location where data is stored. It is like a container that holds some value which can be changed during program execution.
_
).name
, Name
, and NAME
are different variables.if
, class
, while
).# Assigning values to variables
x = 10 # integer
name = "Python" # string
pi = 3.14 # float
is_active = True # boolean
print(x) # Output: 10
print(name) # Output: Python
print(pi) # Output: 3.14
print(is_active) # Output: True
x = 5
print(x) # Output: 5
x = "Hello"
print(x) # Output: Hello
π Here, the same variable x
first stores a number and then a string,
which shows Python is dynamically typed.
a = 50
print(id(a)) # Shows unique memory address where 'a' is stored
# Example 1: Assigning values to variables
x = 10
name = "Python"
pi = 3.14
is_active = True
print(x) # Output: 10
print(name) # Output: Python
print(pi) # Output: 3.14
print(is_active) # Output: True
# Example 2: Changing variable values
x = 5
print(x) # Output: 5
x = "Hello"
print(x) # Output: Hello
# Example 3: Getting memory address of a variable
a = 50
print(id(a)) # Shows unique memory address of 'a'
In Python, data types define the type of value a variable can hold. Python is dynamically typed, which means you donβt need to declare the type explicitly β it is decided at runtime.
int
, float
, complex
str
, list
, tuple
dict
set
, frozenset
bool
bytes
, bytearray
, memoryview
NoneType
# Numeric Types
x = 10 # int
y = 3.14 # float
z = 2+3j # complex
print("x is", x, "Type:", type(x))
print("y is", y, "Type:", type(y))
print("z is", z, "Type:", type(z))
# Sequence Types
name = "Python" # String
fruits = ["apple", "banana", "cherry"] # List
numbers = (1, 2, 3) # Tuple
print(name, type(name))
print(fruits, type(fruits))
print(numbers, type(numbers))
# Dictionary
student = {
"name": "Alice",
"age": 21,
"grade": "A"
}
print(student)
print("Name:", student["name"])
# Boolean and NoneType
is_active = True
is_admin = False
nothing = None
print(is_active, type(is_active))
print(is_admin, type(is_admin))
print(nothing, type(nothing))
In Python, every value has a specific data type. It tells the interpreter how the value should be stored and what operations can be performed on it.
# Numeric Types
x = 10 # int
y = 3.14 # float
z = 2+3j # complex
print(x, type(x))
print(y, type(y))
print(z, type(z))
A string is a sequence of characters enclosed in single (' '), double (" ") or triple (''' ''' / """ """) quotes.
# String Example
name = "Python"
greet = 'Hello'
multiline = """This
is
multiline"""
print(name)
print(greet)
print(multiline)
A list is an ordered, mutable (changeable) collection of items. It can hold different data types in one list.
# List Example
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4]
fruits.append("mango")
print(fruits)
print(numbers[2])
A tuple is an ordered, immutable (unchangeable) collection of items. Once created, elements cannot be modified.
# Tuple Example
coordinates = (10, 20)
colors = ("red", "blue", "green")
print(coordinates)
print(colors[1])
A dictionary is an unordered collection of key-value pairs. Keys must be unique and immutable.
# Dictionary Example
student = {
"name": "Alice",
"age": 21,
"grade": "A"
}
print(student)
print(student["name"])
A set is an unordered collection of unique items (no duplicates).
# Set Example
numbers = {1, 2, 2, 3}
letters = set(['a', 'b', 'c'])
print(numbers) # duplicates removed
print(letters)
A boolean represents one of two values: True
or False
.
# Boolean Example
is_active = True
is_admin = False
print(is_active)
print(is_admin)
None represents the absence of a value or a null value. Its type is NoneType
.
# None Example
result = None
print(result)
print(type(result))
Python supports many mathematical operations. These operators are used to perform calculations on numbers.
Operator | Symbol | Description | Example |
---|---|---|---|
Addition | + | Adds two numbers | 10 + 5 = 15 |
Subtraction | - | Subtracts right operand from left | 10 - 5 = 5 |
Multiplication | * | Multiplies two numbers | 10 * 5 = 50 |
Division | / | Divides left by right (always float result) | 10 / 5 = 2.0 |
Floor Division | // | Divides and gives integer part only | 10 // 3 = 3 |
Modulus | % | Returns remainder | 10 % 3 = 1 |
Exponent | ** | Power (xy) | 2 ** 3 = 8 |
# Addition
a = 10
b = 5
print("Sum:", a + b) # Output: 15
# Subtraction
x = 20
y = 8
print("Difference:", x - y) # Output: 12
# Multiplication
m = 6
n = 7
print("Product:", m * n) # Output: 42
# Division and Floor Division
a = 10
b = 3
print("Division:", a / b) # Output: 3.333...
print("Floor Division:", a // b) # Output: 3
# Modulus and Exponent
print(10 % 3) # Output: 1
print(2 ** 5) # Output: 32
# Program 3: Check Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")
This program checks whether a number entered by the user is even or odd.
The user inputs a number, then the program uses the modulus operator (%
) to check divisibility by 2.
num = int(input("Enter a number: "))
β Takes input from the user, converts it into an integer, and stores it in num.if num % 2 == 0:
β If the number is divisible by 2, it is even.else:
β Otherwise, the number is odd.print()
β Displays whether the number is even or odd.
Example Outputs:
If input is 10 β Output: 10 is Even
If input is 7 β Output: 7 is Odd
# Program 4: Multiplication Table
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
# Program 5: Fibonacci Series
def fibonacci(n):
a, b = 0, 1
count = 0
while count < n:
print(a, end=" ")
nth = a + b
a = b
b = nth
count += 1
fibonacci(10)
# Python program to display Fibonacci series
def fibonacci(n):
# First two terms
a, b = 0, 1
count = 0
# Check if the number of terms is valid
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence up to", n, ":")
print(a)
else:
print("Fibonacci sequence:")
while count < n:
print(a, end=' ')
nth = a + b
a = b
b = nth
count += 1
# Call the function
fibonacci(10)
Python has a straightforward syntax that emphasizes readability and reduces the cost of program maintenance.
Python's standard library is large and comprehensive, offering modules for everything from web development to data science.
Python has a large and active community that contributes to an extensive ecosystem of frameworks and tools.
HTML, CSS, and JavaScript are the core technologies of web content. HTML provides the structure, CSS the styling, and JavaScript the behavior.
<!-- Simple HTML page with CSS and JavaScript -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.btn {
background: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello World!</h1>
<button class="btn" id="myBtn">Click Me</button>
</div>
<script>
// JavaScript code
document.getElementById('myBtn').addEventListener('click', function() {
alert('Button clicked!');
});
</script>
</body>
</html>
HTML (HyperText Markup Language) is the standard markup language for documents designed to be displayed in a web browser.
CSS (Cascading Style Sheets) is used to describe the presentation of a document written in HTML or XML.
JavaScript is a programming language that enables interactive web pages and is an essential part of web applications.
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.
// Java program to demonstrate a simple class
public class Student {
// Fields
private String name;
private int age;
// Constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// Method to display student information
public void displayInfo() {
System.out.println("Student Name: " + this.name);
System.out.println("Student Age: " + this.age);
}
// Main method
public static void main(String[] args) {
// Create a Student object
Student student1 = new Student("John Doe", 20);
// Call display method
student1.displayInfo();
}
}
Java is fundamentally object-oriented, which helps in creating modular programs and reusable code.
Java code is compiled into bytecode that can run on any device with a Java Virtual Machine (JVM).
Java's Just-In-Time (JIT) compiler enables high performance by compiling bytecode to native machine code at runtime.