Python Jan 2025
Python Jan 2025
Syntax and semantics: Programming languages have a unique syntax and semantics, which are
usually defined by a formal language.
Vocabulary: Each programming language has a unique set of keywords that follow a special
syntax.
Features: Programming languages usually have features like variables, a type system, and
mechanisms for error handling.
Implementation: To execute programs, you need an implementation of a programming language,
such as a compiler or an interpreter.
There are many programming languages, but only a few are widely used. Some examples of
programming languages include: Python, JavaScript, Java, C, and SQL.
What do you understand by Python Language?
Python is the text-based programming language used by millions of professional coders at places
like Google, IBM, and even NASA!
//A program in C
language
#include<stdio.h> 000010
int main()
COMPILER
111100 Hello World
OR
{ 1010101011
INTERPRETER
printf(“Hello World”); 1010100011
Return 0;
}
Differentiate between Compiler and Interpreter
Interpreter Complier
Translates program one statement at a Scans the entire program and translates
time. it as a whole into machine code.
Compilers usually take a large amount
Interpreters usually take less amount of time to analyze the source code.
of time to analyze the source code. However, the overall execution time is
However, the overall execution time is comparatively faster than interpreters.
comparatively slower than compilers.
Python accepts single ('), double (") and triple (""" or """) quotes to denote string literals.
The triple quotes are used to span the string across multiple lines.
Example
Word = ‘word’
Sentence = “This is a sentence.”
Paragraph = ‘‘‘ This is a paragraph. It is
made up of multiple lines
and sentences. ’’’
Variables in Python
Variable: In Python, variables are used to store data that can be referenced and manipulated
throughout a program. For Example:
Variable = Data #Datatype
Age = 25 # Integer
Name = "Alice" # String
Height = 5.7 # Float
is_student = True # Boolean
Note: Make sure the number of variables matches the number of values, or else you will get an
error.
Different ways to assign variables
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values into
variables. This is called unpacking.
Example: Unpack a list:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
Explain any four data-types available in python.
Integer(int):integers are whole numbers without decimal point. They can be positive or
negative
Ex:793,-254,4
Floating-Point numbers: Floats are written with a decimal point that segregates the
integer from fractional number.
Ex:6.2e3 , 6200.2
len() function: returns the length of an object or the number of item in a list given as argument.
>>> s=("sun","moon","star")
>>> len(s)
3
>>> len("Mayo college")
12
Lab Exercise
To print “Hello World ”
print("Hello, World!")
print("Fibonacci Sequence:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
To reverse a string
text = input("Enter a string: ")
reversed_text = text[::-1]
print("Reversed string:", reversed_text)
To reverse a numbers
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num //= 10
print("Reversed number:", reverse)
Write a program to obtain length of a square and calculate its area and perimeter.
l=int(input("Enter length:"))
area=l*l
peri=4*l
print("area=",area)
print("perimeter=",peri)
Practice Question
• Write a program to take input of three numbers from user and print the
average and sum.
• Write a program which takes input of radius of circle and print its perimeter
• area=3.14*r*r
• Write a program which takes input of length and breadth from user of a
rectangle and calculate its area and perimeter.
area=length*breadth
peri=a*(length+breadth)