0% found this document useful (0 votes)
2 views32 pages

PYTHON

The document provides an overview of Python programming, covering interactive and script modes, comments, printing statements, input functions, keywords, identifiers, variables, data types, and conditional statements. It includes examples and exercises for practical understanding, such as calculating areas, checking eligibility to vote, and performing basic arithmetic operations. The content is structured to facilitate learning for beginners in Python programming.

Uploaded by

69172
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views32 pages

PYTHON

The document provides an overview of Python programming, covering interactive and script modes, comments, printing statements, input functions, keywords, identifiers, variables, data types, and conditional statements. It includes examples and exercises for practical understanding, such as calculating areas, checking eligibility to vote, and performing basic arithmetic operations. The content is structured to facilitate learning for beginners in Python programming.

Uploaded by

69172
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

PYTHON

INTERACTIVE MODE
• This mode allows the user to directly run commands on the Python
interpreter prompt denoted by “>>>”.

• The interactive mode allows you to enter commands or statements


and Python executes them immediately, providing instant feedback.

• It is a conversation with Python, where you can try snippets of code,


test functions.
TRY TO RUN THE FOLLOWING AND OBSERVE THE
RESULT:

>>> 34+67
>>> 2*3*4*5
>>> “Hello” + “Python”
>>> 4 + 9 * 3 + 8
>>>print (“I love Python”)
SCRIPT MODE
• Script mode allows us to save the set of commands or programs we
type.

• It is preferred mode for beginners because it allows for combining


multiple statements into complete programs or functions.

• It is a built for programming.


SCRIPT MODE
• Open Python IDLE : Launch Python IDLE. Go to the File menu and
select New File or press Ctrl+N to open a new editor window.

• Write your Python Code: In the editor, write your Python code,
including any functions, variable or statements you want in your
program.

• Save the File : Go to the File menu and select save or press CTRL+S to
save your python file. Make sure to use the .py extension.

• Run the Script : Run your script by going to Run menu or simply
pressing the F5 key.
COMMENTS IN PYTHON
• Comments in any programming language are non-executable
statements in the code.

• Comments are used to label the code, add explanations, make it more
understandable and help others comprehend the code’s logic. These
are ignored by the Python interpreter and do not affect the execution
of code.
COMMENTS IN PYTHON
SINGLE – LINE COMMENTS:
• These are comments that span only one line and are often used for
short explanations or notes.
• These comments ae added to Python programs using the “#” symbol.

EXAMPLES:

• Print (“Hello, World!”) # This line prints a greeting


COMMENTS IN PYTHON
MULTI – LINE COMMENTS:
• Multiline comments are indicated by adding a hash (#) sign before
every line or enclosing multiple line in triple quotes, either triple
single quotes (‘’’) or tiple double quotes (“””).

EXAMPLES:

• ‘’’This is multiline
comments’’’
print (“Hello, World!”)
PRINTING STATEMENTS
• Python’s print() function is used to print data to the output device.

EXAMPLES:

• print (“Hello World”)

When you want to print a variable value using print(), you do not enclose
it in any quotes.

fruit=“apples”
print(“I love”,fruit)
Output – I love apples
USING INPUT() FUNCTION
• Python’s input () function is used to input data by the user.

EXAMPLES:

• To accept an integer input from the user:


age=int(input(“Enter your age:”))
print(age)

• To accept a string input from the user:


name=str(input(“Enter your name:”))
print(name)
EXAMPLES:

• To accept a decimal value input from the user:


marks=float(input(“Enter your marks:”))
print(marks)
KEYWORDS
• In Python, "keywords" (also called reserved words) are special words
that have predefined meanings in the language and cannot be used for
other purposes, such as naming variables, functions, or classes.
IDENTIFIERS
• Identifiers are the names used to identify elements in Python, such as
variables, functions, classes, modules, etc.
• Identifiers are just names that label variables, functions, and other
objects in code.

my_variable = 10
# "my_variable" is an identifier for the variable storing the value 10.
RULES FOR WRITING IDENTIFIERS
• Identifiers can be combination of alphanumeric characters (a-z, A-Z, 0-
9) and underscores (_). They cannot contain special characters, such
as @, -, #, *, %
Valid Examples : hello123, _age, _name_1
Invalid Examples: name@, #Age, Total$

• An identifier name must start with a letter or the underscore


character and never with digits.

Valid Examples : Total, sum, _Total_Marks


Invalid Examples: $Sum, 100_Name, Total-Marks
RULES FOR WRITING IDENTIFIERS

• Python is a case-sensitive language. So, Variable names are also case-


sensitive (total, Total and TOTAL are three different variables)

• Keywords cannot be used as identifiers.


VARIABLES
• The container which stores the value. Variables are reserved memory
locations where values can be stored.

x=5 # "x" is a variable storing the integer value 5.


name = "Alice" # "name" is a variable storing the string value "Alice".

All variables are identifiers, but not all identifiers are variables (e.g., function names, class names).
DATA TYPES
• Every variable that we use in our programs can hold a value of a certain
data type. The categorization of data items is known as data types.
DATA TYPES

Numeric Dictionary Boolean Set Sequence Type

Integer Strings

Complex Number List

Float Tuple
Integer – Any number from 0 to infinity, both positive and negative numbers.
Complex Numbers - numbers that have both a real part and an imaginary part.
example: z = 3 + 4j
Here, 3 is the real part, and 4j is the imaginary part.
Float – Decimal Values
String – Textual data, such as “Apples are good for health”
Boolean – True/False or 1/0
Lists – Stores a collection of values
Set – Unordered collection of values that has no duplicate values and is
mutable.
Dictionary – Stores data in key-value pairs.
Tuples – Similar to Lists, but they are immutable.
CONDITIONAL STATEMENTS
• Conditional statements are used for selecting the block of statements
to be executed based on the condition.

The syntax of the if-else statement is:


if <conditional expression>:
statement
else:
statement
CONDITIONAL STATEMENTS
• Example:

if a>=0:
print(a, “is zero or a positive number”)
else:
print(a, “is a negative number”)
Question: Write a program to input age from user and print if as
per the input-age, one is eligible to vote or not.
Solution:

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


if age>=18:
print(“18 and more, you are eligible to vote”)
else:
print(“under 18, you are not eligible to vote”)
Question: Write a program to input two numbers and check if
the two numbers are equal or not.
Solution:

a =int(input(“Enter number1:”))
b=int(input(“Enter number2:”))
if a==b:
print(“Both numbers are equal”)
else:
print(“Numbers are not equal”)
Question: Write a program that takes a number and check
whether the given number is odd or even.
number = int(input("Enter an integer: "))

# Check if the number is even or odd


if number % 2 == 0:
print("Even")
else:
print("Odd")
Question: Simple Calculator
Write a program that asks the user for two numbers and an
operator (+, -, *, /) and performs the corresponding operation.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Enter the operator (+, -, *, /): ")

if operator == '+': The f in front of the string is used to


create an f-string, which allows for
print(f"The result is: {num1 + num2}") string interpolation.
elif operator == '-':
print(f"The result is: {num1 - num2}")
elif operator == '*':
print(f"The result is: {num1 * num2}")
elif operator == '/':
print(f"The result is: {num1 / num2}") The {} brackets are placeholders for variables or
else: expressions inside an f-string.
print("Invalid operator!")
x=5
y = 10
print(f"The sum of {x} and {y} is {x + y}.")
Question: Grade Classification
Write a program that asks for a student's grade (out of 100) and
prints the classification:

"A" if grade is between 90 and 100,


"B" if grade is between 80 and 89,
"C" if grade is between 70 and 79,
"D" if grade is between 60 and 69,
"F" if grade is less than 60.
grade = int(input("Enter your grade (out of 100): "))

if grade >= 90:


print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")
Question:
i) Calculate the Area of a Parallelogram
ii) Calculate the Volume of a Sphere
iii) Calculate the Surface Area of a Cylinder
iv) Write python program to Check if a Number is Positive,
Negative, or Zero
v) Write python program to Find the Largest of Two Numbers

You might also like