Complete Python Programming Guide with Outputs
Professional Beginner-to-Intermediate Python Learning Guide
Introduction
Python is a high-level, interpreted programming language known for its simplicity, readability,
and versatility. It is widely used in software development, automation, web development,
machine learning, data science, and artificial intelligence. This document covers Python
fundamentals with definitions, syntax, examples, and outputs.
1. Variables
Variables are containers used to store data values in Python. Python automatically determines
the type of variable based on the assigned value.
Variable Declaration
Definition:
Variables are created when a value is assigned.
Syntax:
variable_name = value
Example 1:
name = "Rajesh"
age = 25
print(name)
print(age)
Output:
Rajesh
25
Example 2:
x = 10
y = 20
print(x + y)
Output:
30
2. Data Types
Data types define the type of data stored in variables. Python provides several built-in data
types.
Numeric Data Types
Definition:
Numeric data types include int, float, and complex numbers.
Syntax:
variable = value
Example 1:
x = 10
y = 10.5
print(type(x))
print(type(y))
Output:
<class 'int'>
<class 'float'>
Example 2:
z = 2 + 3j
print(type(z))
Output:
<class 'complex'>
Boolean Data Type
Definition:
Boolean values are either True or False.
Syntax:
variable = True / False
Example 1:
is_valid = True
print(is_valid)
Output:
True
Example 2:
print(10 > 5)
Output:
True
3. Strings
Strings are sequences of characters enclosed inside quotes. They are widely used to store
textual information.
String Basics
Definition:
Strings can be enclosed using single, double, or triple quotes.
Syntax:
name = "Python"
Example 1:
name = "Rajesh"
city = "Hyderabad"
print(name)
print(city)
Output:
Rajesh
Hyderabad
Example 2:
message = """Hello
World"""
print(message)
Output:
Hello
World
String Operations
Definition:
Python supports concatenation, repetition, and membership operations.
Syntax:
string1 + string2
Example 1:
first = "Hello"
second = "World"
print(first + " " + second)
Output:
Hello World
Example 2:
print("Python " * 3)
Output:
Python Python Python
Example 3:
msg = "Python is easy"
print("Python" in msg)
Output:
True
String Methods
Definition:
Python provides built-in methods for string manipulation.
Syntax:
[Link]()
Example 1:
text = "python"
print([Link]())
Output:
PYTHON
Example 2:
text = " Hello "
print([Link]())
Output:
Hello
Example 3:
text = "Hello World"
print([Link]("World", "Python"))
Output:
Hello Python
4. Conditional Logic
Conditional statements allow programs to make decisions based on conditions.
if, elif, else
Definition:
Used to execute different blocks of code based on conditions.
Syntax:
if condition:
statements
Example 1:
age = 20
if age >= 18:
print("Adult")
Output:
Adult
Example 2:
marks = 80
if marks >= 90:
print("A")
elif marks >= 75:
print("B")
else:
print("C")
Output:
B
Ternary Operator
Definition:
Short form of if-else condition.
Syntax:
value_if_true if condition else value_if_false
Example 1:
age = 16
result = "Adult" if age >= 18 else "Minor"
print(result)
Output:
Minor
5. Loops
Loops are used to repeat a block of code multiple times.
for Loop
Definition:
Used to iterate through sequences.
Syntax:
for variable in sequence:
Example 1:
for i in range(3):
print(i)
Output:
0
1
2
Example 2:
name = "Python"
for char in name:
print(char)
Output:
P
y
t
h
o
n
while Loop
Definition:
Executes repeatedly until condition becomes False.
Syntax:
while condition:
Example 1:
count = 1
while count <= 3:
print(count)
count += 1
Output:
1
2
3
break and continue
Definition:
Used to control loop execution.
Syntax:
break / continue
Example 1:
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2
Example 2:
for i in range(5):
if i == 2:
continue
print(i)
Output:
0
1
3
4
6. Lists
Lists are ordered and mutable collections that allow duplicate values.
List Basics
Definition:
Lists store multiple values in a single variable.
Syntax:
list_name = [item1, item2]
Example 1:
numbers = [10,20,30]
print(numbers)
Output:
[10, 20, 30]
Example 2:
fruits = ["apple","banana"]
[Link]("orange")
print(fruits)
Output:
['apple', 'banana', 'orange']
List Methods
Definition:
Python provides many useful list methods.
Syntax:
[Link]()
Example 1:
nums = [1,2,3]
[Link]()
print(nums)
Output:
[1, 2]
Example 2:
nums = [3,1,2]
[Link]()
print(nums)
Output:
[1, 2, 3]
7. Tuples
Tuples are ordered and immutable collections.
Tuple Basics
Definition:
Tuples cannot be modified after creation.
Syntax:
tuple_name = (item1, item2)
Example 1:
numbers = (10,20,30)
print(numbers)
Output:
(10, 20, 30)
Example 2:
colors = ("red","green")
print(colors[0])
Output:
red
8. Dictionaries
Dictionaries store data using key-value pairs.
Dictionary Basics
Definition:
Keys are unique and used to access values.
Syntax:
dict_name = {key:value}
Example 1:
student = {"name":"Raj","age":22}
print(student["name"])
Output:
Raj
Example 2:
employee = {"id":101,"salary":50000}
employee["salary"] = 60000
print(employee)
Output:
{'id': 101, 'salary': 60000}
9. Sets
Sets store unique values and do not allow duplicates.
Set Basics
Definition:
Sets are useful for removing duplicate values.
Syntax:
set_name = {1,2,3}
Example 1:
nums = {1,2,2,3}
print(nums)
Output:
{1, 2, 3}
Example 2:
a = {1,2,3}
b = {3,4,5}
print([Link](b))
Output:
{1, 2, 3, 4, 5}
10. Functions
Functions are reusable blocks of code used to perform specific tasks.
Function Definition
Definition:
Functions are defined using the def keyword.
Syntax:
def function_name():
Example 1:
def greet():
print("Hello World")
greet()
Output:
Hello World
Parameters and Return Values
Definition:
Functions can take input and return output.
Syntax:
return value
Example 1:
def add(a,b):
return a+b
print(add(2,3))
Output:
5
Example 2:
def square(x):
return x*x
print(square(4))
Output:
16
Lambda Functions
Definition:
Anonymous single-line functions.
Syntax:
lambda arguments : expression
Example 1:
square = lambda x : x*x
print(square(5))
Output:
25
11. Object-Oriented Programming (OOP)
Object-Oriented Programming organizes code using classes and objects.
Classes and Objects
Definition:
Classes are blueprints, objects are instances.
Syntax:
class ClassName:
Example 1:
class Student:
pass
s1 = Student()
print(type(s1))
Output:
<class '__main__.Student'>
Constructor (__init__)
Definition:
The constructor initializes object data automatically.
Syntax:
def __init__(self, parameters):
Example 1:
class Student:
def __init__(self, name):
[Link] = name
s1 = Student("Raj")
print([Link])
Output:
Raj
Methods and Attributes
Definition:
Methods define behavior and attributes store data.
Syntax:
[Link]
Example 1:
class Student:
def __init__(self,name):
[Link] = name
def greet(self):
print("Hello", [Link])
s1 = Student("Raj")
[Link]()
Output:
Hello Raj
Inheritance
Definition:
Inheritance allows one class to inherit another class properties.
Syntax:
class Child(Parent):
Example 1:
class Parent:
def show(self):
print("Parent Class")
class Child(Parent):
pass
c = Child()
[Link]()
Output:
Parent Class
Conclusion
This document covered Python fundamentals including variables, data types, strings, conditional
statements, loops, collections, functions, and Object-Oriented Programming concepts with
examples and outputs. These concepts form the foundation for advanced Python programming.