0% found this document useful (0 votes)
13 views4 pages

Python Programming Basics Guide

This document provides an introductory guide to Python programming, covering topics such as variables, data types, operators, conditional statements, loops, functions, data structures, file handling, exception handling, and object-oriented programming. It includes examples and exercises for practical understanding. The content is designed for beginners and highlights Python's versatility in various applications.

Uploaded by

Safi Nilu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Python Programming Basics Guide

This document provides an introductory guide to Python programming, covering topics such as variables, data types, operators, conditional statements, loops, functions, data structures, file handling, exception handling, and object-oriented programming. It includes examples and exercises for practical understanding. The content is designed for beginners and highlights Python's versatility in various applications.

Uploaded by

Safi Nilu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming Notes multi - line comment

"""
Prepared by Dr. Subhendu Das
Assistant Professor, GGDC Chapra
3rd January, 2025 2.2 Variables
Variables store data. You can assign values using the = oper-
1 Introduction to Python ator.
x = 10 # integer
Python is a high-level, interpreted, general-purpose program- name = " Jimmy " # string
ming language. It is easy to read and write, making it pi = 3.14 # float
beginner-friendly. Python is widely used in: is_active = True # boolean

• Data Science and Machine Learning


2.3 Data Types
• Web Development (Flask, Django)
• Integer (int): whole numbers, e.g., 5, -10
• Automation and Scripting
• Floating Point (float): decimals, e.g., 3.14, -0.5
• Game Development
• String (str): text, e.g., ”Hello”
• Internet of Things (IoT)
• Boolean (bool): True or False
1.1 First Program
• None: represents no value
Your first Python program prints a message on the screen.
print ( " Hello , ␣ World ! " ) x = 5
print ( type ( x ) ) # < class ’ int ’>
Exercise: Print your name and age using print(). print ( type ( pi ) ) # < class ’ float ’>
print ( type ( name ) ) # < class ’ str ’>
2 Python Basics print ( type ( is_active ) ) # < class ’ bool ’>

Exercise: Create variables of all data types and print their


2.1 Comments types.
Comments are non-executable lines used to explain code.
# This is a single - line comment 3 Operators
""" 3.1 Arithmetic Operators
This is a

1
a , b = 10 , 3 name = input ( " Enter ␣ your ␣ name : ␣ " )
print ( a + b ) # 13 age = int ( input ( " Enter ␣ your ␣ age : ␣ " ) ) #
print ( a - b ) # 7 converting string to int
print ( a * b ) # 30 print ( " Hello , " , name )
print ( a / b ) # 3.3333 print ( " You ␣ are " , age , " years ␣ old " )
print ( a // b ) # 3 ( floor division )
Exercise: Write a program to add two numbers entered by
print ( a % b ) # 1 ( modulus )
print ( a ** b ) # 1000 ( power ) the user.

5 Conditional Statements
3.2 Comparison Operators
Conditional statements allow decision-making.
a , b = 5 , 10
print ( a == b ) # False age = 20
print ( a != b ) # True if age >= 18:
print ( a > b ) # False print ( " Adult " )
print ( a < b ) # True elif age > 12:
print ( " Teenager " )
else :
print ( " Child " )
3.3 Logical Operators
Exercise: Write a program to check if a number is positive,
x = True negative, or zero.
y = False
print ( x and y ) # False
print ( x or y ) # True 6 Loops
print ( not x ) # False
6.1 While Loop

3.4 Assignment Operators i = 1


while i <= 5:
a = 5 print ( i )
a += 3 # a = a + 3 -> 8 i += 1
a *= 2 # a = a * 2 -> 16

Exercise: Write a program to calculate area and perimeter 6.2 For Loop
of a rectangle using variables and operators.
for i in range (1 , 6) :
print ( i )
4 Input and Output Keywords: break, continue, pass
Exercise: Print all even numbers from 1 to 20 using loops.

2
7 Functions 8.4 Dictionary

Functions are reusable blocks of code. Key-value pairs, unordered, mutable.

def greet ( name ) : student = { " name " : " Jimmy " , " age " : 30}
return " Hello ␣ " + name student [ " age " ] = 31
print ( student [ " name " ])
print ( greet ( " Jimmy " ) )
Exercise: Create a dictionary of 3 students with name and
Exercise: Create a function to calculate the factorial of a age. Print all names.
number.

9 Strings
8 Data Structures
s = " Python "
8.1 List
print ( s . upper () ) # PYTHON
Ordered, mutable collection of items. print ( s . lower () ) # python
print ( s [0:3]) # Pyt
fruits = [ " apple " , " banana " , " cherry " ] print ( " thon " in s ) # True
fruits . append ( " mango " ) # add element print ( s . replace ( " Python " , " Java " ) )
fruits . remove ( " banana " ) # remove element
print ( fruits [0]) # apple Exercise: Count number of vowels in a string.

8.2 Tuple 10 File Handling


Ordered, immutable collection.
# Writing to a file
point = (3 , 4) f = open ( " test . txt " , " w " )
print ( point [0]) f . write ( " Hello , ␣ file ! " )
f . close ()

# Reading a file
8.3 Set f = open ( " test . txt " , " r " )
print ( f . read () )
Unordered, mutable, no duplicates.
f . close ()
s = {1 , 2 , 3 , 3}
print ( s ) # {1 , 2 , 3} Exercise: Write a program to count the number of lines in
a file.

3
11 Exception Handling 14 Advanced Topics
• List Comprehension: [x**2 for x in range(5)]
try :
x = 10 / 0 • Lambda Functions: f = lambda x: x*2
except Zer oDivis ionErr or :
print ( " Cannot ␣ divide ␣ by ␣ zero " ) • Iterators and Generators
finally :
print ( " Done " ) • Virtual Environments
Exercise: Handle input errors when a user enters a non- • pip (Python package manager)
number.

15 Next Steps
12 Object-Oriented Programming • Work on small projects: Calculator, To-Do App, Web
Scraper
class Student :
def __init__ ( self , name , age ) : • Learn libraries: NumPy, Pandas, Matplotlib
self . name = name
self . age = age • Learn frameworks: Flask, Django

def greet ( self ) : • Practice coding daily


print ( " Hello , " , self . name )

s1 = Student ( " Jimmy " , 30)


s1 . greet ()

Exercise: Create a class Rectangle with methods to cal-


culate area and perimeter.

13 Modules and Packages

import math
print ( math . sqrt (16) ) # 4.0

from math import pi


print ( pi )

You might also like