Python Programming

python fundament

Python Fundamentals


Related Youtube Video

youtube link: Padma Python Fundamental


What is Python?

Python is a high-level, interpreted, and easy-to-learn programming language. It is widely used for:

  • Web Development
  • Data Science
  • Machine Learning
  • Automation
  • Desktop Applications
  • Game Development
  • Scripting

Example

print("Hello, World!")

Output:

Hello, World!

Python Statements

A statement is an instruction that Python can execute.

Simple Statement

x = 10
name = "John"
print(x)

Multiple Statements

x = 10
y = 20
z = x + y
print(z)

Output:

30

Python Comments

Comments are used to explain code and are ignored by Python.

Single-Line Comment

# This is a comment
print("Hello")

Inline Comment

x = 10  # Store value in x

Multi-Line Comment

Python does not have a dedicated multi-line comment syntax, but triple quotes are often used.

"""
This is a
multi-line comment
"""
print("Hello")

Common Comment Mistakes

Mistake 1: Forgetting

❌ Wrong

This is a comment
print("Hello")

Output:

SyntaxError

✅ Correct

# This is a comment
print("Hello")

Mistake 2: Using Comments as Code

❌ Wrong

# print("Hello")

Output:

Nothing is printed

✅ Correct

print("Hello")

Python Indentation

Python uses indentation (spaces or tabs) to define blocks of code.

Correct Indentation

age = 18

if age >= 18:
    print("Adult")

Output:

Adult

Incorrect Indentation

❌ Wrong

age = 18

if age >= 18:
print("Adult")

Output:

IndentationError

✅ Correct

age = 18

if age >= 18:
    print("Adult")

Indentation Levels Example

age = 20

if age >= 18:
    print("Adult")

    if age >= 60:
        print("Senior Citizen")
    else:
        print("Not Senior Citizen")

Output:

Adult
Not Senior Citizen

Common Indentation Mistakes

Mistake 1: Missing Indentation

❌ Wrong

if True:
print("Hello")

Output:

IndentationError

Mistake 2: Mixing Tabs and Spaces

❌ Wrong

if True:
    print("Hello")
    print("World")

Output:

TabError: inconsistent use of tabs and spaces

✅ Correct

if True:
    print("Hello")
    print("World")

Python Input

The input() function is used to get data from the user.

Basic Input

name = input("Enter your name: ")

print(name)

Input:

John

Output:

John

Input Always Returns String

age = input("Enter age: ")

print(type(age))

Input:

25

Output:

<class 'str'>

Converting Input Types

String to Integer

age = int(input("Enter age: "))

print(age)

Input:

25

Output:

25

String to Float

price = float(input("Enter price: "))

print(price)

Input:

99.99

Output:

99.99

Common Input Mistakes

Mistake 1: Forgetting Type Conversion

❌ Wrong

num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

print(num1 + num2)

Input:

10
20

Output:

1020

Explanation:

Both values are strings, so Python joins them.

✅ Correct

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print(num1 + num2)

Output:

30

Mistake 2: Invalid Integer Conversion

❌ Wrong

age = int(input("Enter age: "))

Input:

twenty

Output:

ValueError

✅ Better

try:
    age = int(input("Enter age: "))
    print(age)
except ValueError:
    print("Please enter a valid number.")

Python Output

The print() function displays output on the screen.

Basic Output

print("Hello")

Output:

Hello

Multiple Values

name = "John"
age = 25

print(name, age)

Output:

John 25

name = "John"
age = 25

print(f"My name is {name} and I am {age} years old.")

Output:

My name is John and I am 25 years old.

Common Output Mistakes

Mistake 1: Missing Quotes

❌ Wrong

print(Hello)

Output:

NameError: name 'Hello' is not defined

✅ Correct

print("Hello")

Mistake 2: Wrong Variable Name

❌ Wrong

name = "John"

print(Name)

Output:

NameError

Explanation:

Python is case-sensitive.

✅ Correct

name = "John"

print(name)

Beginner Checklist

Before running your code, check:

  • Did I use proper indentation?
  • Did I close all quotes?
  • Did I spell variable names correctly?
  • Did I convert input to the correct type?
  • Did I use : after if, for, while, def, and class?
  • Am I mixing tabs and spaces?
  • Did I accidentally comment out code?
  • Did I use matching parentheses ()?

Quick Practice Exercise

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

print(f"Hello {name}")
print(f"You are {age} years old")

Sample Input:

Rahul
21

Sample Output:

Hello Rahul
You are 21 years old

Summary

Topic Purpose
Statement Executes an instruction
Comment Explains code
Indentation Defines code blocks
input() Takes user input
print() Displays output
int() Converts string to integer
float() Converts string to decimal
f-string Formats output neatly

Master these fundamentals before moving to:

  1. Variables
  2. Data Types
  3. Operators
  4. Conditional Statements
  5. Loops
  6. Functions
  7. Object-Oriented Programming (OOP)

Filters

No filters available for this view.

Reset All