PYTHON
PYTHON
INTERACTIVE MODE
• This mode allows the user to directly run commands on the Python
interpreter prompt denoted by “>>>”.
>>> 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.
• 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:
EXAMPLES:
• ‘’’This is multiline
comments’’’
print (“Hello, World!”)
PRINTING STATEMENTS
• Python’s print() function is used to print data to the output device.
EXAMPLES:
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:
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$
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
Integer Strings
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.
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:
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: "))