Programming in Python: WWW - Iitkirba.xyz
Programming in Python: WWW - Iitkirba.xyz
www.iitkirba.xyz
Module-1
• Introduction to Python , features and applications.
• Datatypes , Keywords , Identifiers , Literals , Constants, Variables.
• Python indentation , Concept of indentation.
• Operators and Expressions , Naming conventions, Managing input & output.
• Conditional Statements , Looping statements , Nesting of loops.
• break , continue , pass & return statements.
www.iitkirba.xyz
Introduction to Python
• High-level, interpreted & general-purpose programming language.
• Created by Guido van Rossum in 1991 and further developed by
the Python Software Foundation.
• It was designed with an emphasis on code readability.
• Helps the programmers to write the code in fewer lines.
• A programming language that lets you work quickly and integrate
systems more efficiently.
www.iitkirba.xyz
Features of Python
• Easy to use and Read
• Dynamically Typed
• High-level
• Compiled and Interpreted Garbage Collected
• Purely Object-Oriented
• Cross-platform Compatibility
• Rich Standard Library
www.iitkirba.xyz
Applications of Python
• Data Science
• Desktop Applications
• Console-based Applications
• Mobile Applications ,Web Applications & Software Development
• Machine Learning
• Computer Vision or Image Processing Applications & Speech Recognition
• Testing
• Gaming
www.iitkirba.xyz
Variables
Variables are used to store data that can be referenced and
manipulated during program execution. A variable is essentially a
name that is assigned to a value. Unlike many other programming
languages, Python variables do not require explicit declaration of
type. The type of the variable is inferred based on the value assigned.
www.iitkirba.xyz
Rules for naming variables
www.iitkirba.xyz
Module-1
www.iitkirba.xyz
Data types
Data types in Python define the type of data a variable can hold.
Common types include integers, floats, strings, sequence,
mapping, set types,binary types ,tuple,List and Boolean . Each
type has unique properties and behaviors, influencing how data
is m anipulated and stored in your prog ram s.
www.iitkirba.xyz
Keywords & Identifiers
• In Python, keywords are
reserved words that have s pecific
m eaning s ,s uch as if,else,and def.
Identifiers, on the other hand, are
names you choos e forvariables
and functions .
• U nders tanding the
dis tinction is vital for writing
syntactically correct code.
www.iitkirba.xyz
Literals & Constants
•Literals are raw data values directly assigned to variables.
•They can be numeric (int, float, complex), string, Boolean (True, False), or special
(None).
•Constants in Python are variables whose values remain unchanged
throughout the program
•Example:PI = 3.14159
www.iitkirba.xyz
Python Indentation
www.iitkirba.xyz
Concept of Indentation
• Python Indentation is the whitespace (space or tab) before a block of
code. It is the cornerstone of any Python syntax and is not just a
convention but a requirement. In other words, indentation signifies that
every statement with the same space to the right is a part of the same
code block.
• Indentation is significant in Python as it ensures code readability.
Unlike C, C++, and other languages, where curly braces represent a
block of code, Python uses indentation level (number of leading
whitespaces) to show a black with the same group of statements.
www.iitkirba.xyz
Module-1
www.iitkirba.xyz
Operators
Definition:Operators are symbols or keywords used to perform operations on values and variables in Python.
Types of Operators:
+, -, *, /, %, **, //
in, not in
is, is not
www.iitkirba.xyz
Naming Conventions
Best Practices:
1. Use meaningful names (age, total_price).
2. Use snake_case for variables and functions (my_variable).
3. Use PascalCase for classes (MyClass).
4. Constants in UPPERCASE (MAX_LIMIT).
Code Example: # Variable student_name = "Ray"
# Function def calculate_area(radius):
return 3.14 * radius ** 2
# Class class MyClass:
pass
www.iitkirba.xyz
Input & Output
Input in Python
• With the print() function, you can display output in various formats, while the input() function enables
interaction with users by gathering input during program execution.
Print Output in Python
• At its core, printing output in Python is straightforward, thanks to the print() function. This function
allows us to display text, variables, and expressions on the console. Let’s begin with the basic usage of
the print() function:
• In this example, “Hello, World!” is a string literal enclosed within double quotes. When executed, this
statement will output the text to the console.
Ex-print("Hello, World!")
Output
Hello, World!
www.iitkirba.xyz
Conditional Statements
Conditional Statements are statements in Python that provide a choice for the control flow based on a
condition.
1. If Conditional Statement in Python
If the simple code of block is to be performed if the condition holds then the if statement is used. Here the
condition mentioned holds then the code of the block runs otherwise not.
Syntax
if condition:
# statement
Example
if 10 > 5:
print("10 greater than 5")
www.iitkirba.xyz
If Else Statement
2. If else Conditional Syntax
condition is false.
x=3
if x == 4:
print("Yes")
else:
print("No")
www.iitkirba.xyz
Nested If Else
• Nested if..else means an if-else # if..else chain statement
statement.
if letter == "B":
print("letter is B")
else if statements.
print("letter is C")
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
www.iitkirba.xyz
If-elif-else
The if statements are executed letter = "A"
www.iitkirba.xyz
Ternary Expression
The Python ternary Expression determines if a condition is true or false and then returns the
appropriate value in accordance with the result.
www.iitkirba.xyz
Module-1
www.iitkirba.xyz
Loops
• In programming, the loops are the constructs that repeatedly execute a
piece of code based on the conditions.
• There are two types of loops in Python and these are for and while loops.
• Both of them work by following the below steps:
• 1. Check the condition
2. If True, execute the body of the block under it. And update the
iterator/the value on which the condition is checked.
3. If False, come out of the loop
www.iitkirba.xyz
While Loop
• While loops execute a set of lines of An example of printing numbers from 1
code iteratively till a condition is to 5 is shown below.
satisfied. Once the condition results in
Example of Python while loop:
False, it stops execution, and the part
of the program after the loop starts i=1
executing.
while (i<=5):
• The syntax of the while loop is :
While condition: print(i)
statement(s) i=i+1
www.iitkirba.xyz
For Loop
• For loop in Python works on a Example of Python for loop:
sequence of values. For each value
in the sequence, it executes the loop
for i in range(5):
till it reaches the end of the print(i)
sequence.
• The syntax for the for loop is:
for iterator in sequence:
statement(s)
www.iitkirba.xyz
Nested Loop
Nested for loop
count = 0
print(name, end=' ‘)
count = count + 1
print() www.iitkirba.xyz
Break
The break statement will completely names = ["Rose", "Max", "Nina", "Phillip"]
break out of the current loop,meaning it for name in names:
won’t run any more of the statements
contained print(f"Hello, {name}")
inside of it. if name == "Nina":
break
www.iitkirba.xyz
Continue
• continue works a little differently. for name in names:
• Instead, it goes back to the start of if name != "Nina":
the loop, skipping over any other continue
statements contained within the loop.
print(f"Hello, {name}")
www.iitkirba.xyz
Pass
• Do nothing. Ignore the condition in # An empty loop
which it occurred and proceed to
run the program as usual.
for letter in ‘Rose':
www.iitkirba.xyz
Return
• A return statement is used to end def add(a, b):
www.iitkirba.xyz
Assignment 1
1: Print the first 10 natural numbers using for loop.
2: Python program to print all the even numbers within the given range.
3: Python program to calculate the sum of all numbers from 1 to a given number.
4: Python program to calculate the sum of all the odd numbers within the given range.
5: Python program to print a multiplication table of a given number
6: Python program to display numbers from a list using a for loop.
7: Python program to count the total number of digits in a number.
8: Python program to check if the given string is a palindrome.
9: Python program that accepts a word from the user and reverses it.
10: Python program to check if a given number is an Armstrong number
11: Python program to count the number of even and odd numbers from a series of numbers.
12: Python program to display all numbers within a range except the prime numbers.
13: Python program to get the Fibonacci series between 0 to 50.
14: Python program to find the factorial of a given number.
15: Python program that accepts a string and calculates the number of digits and letters.
16: Write a Python program that iterates the integers from 1 to 25.
17: Python program to check the validity of password input by users.
18: Python program to convert the month name to a number of days.
www.iitkirba.xyz