1. Introduction to Python
Python is a programming language which was created by Guido Van Rossum . It can be used to
follow both procedural approach and object-oriented approach of programming.
2. PYTHON BASICS Comments : Comments are the statements which are incorporated
in the code to give a better understanding of code statements to the
user. There are two types of comments in python.
1. Single Line
2. Multi Line .
Single Line comment A single-line comment is used to add some
explanatory text in the program for better understanding of the next
line.
Multiline comments The multiline comments are written in python
using triple quotes. You can write number lines starting with triple
quotes and end with triple quotes.
3. Introduction
•CHARACTER SET:
Character set is a group of letters or signs which are
valid in a language. For example English has 26, Greek
has 24 characters, Hindi has 44 etc. And Python
Character set includes letters, sign, numbers and
symbols.
• Letters: A-Z, a-z
• Digits:0-9
• Special Symbols:_,+,-,*,/,(,),{,},[,]...etc
• White spaces: blank space, tab, carriage return,
newline etc.
5. 1. Keywords
•Keywords are those reserved
words for specific functioning which
provides a special meaning to
interpreter/compiler.
•The keywords can’t be used for
any other purpose than the originally
defined. Some of the keywords
displayed below:
7. Example on Identifiers
● Identifiers are the name given to variables, classes, methods(functions), etc. For example,
Here, language is a variable (an identifier) which holds the value 'Python'.
We cannot use keywords as variable names as they are reserved names that are
built-in to Python. For example,
The above code is wrong because we have used continue as a variable name.
10. Activity: W.A.P to declare four different type of values Example:
Value 1="Your Name"
Value 2="Age"
Value 3="Grade in Roman Number" Use print statement "sep"
and "end", to add more data.
11. ● In Python, a variable is a container for holding values, akin to a box.
● These containers can accommodate a wide range of data types, making them versatile storage units.
● Like a physical box, you can open the variable to inspect its contents or add new data.
● Assigning a name to a variable is comparable to labelling a container, providing a means of
identification.
● These named variables can store various values, such as numbers and strings, in Python.
Variables
12. There are certain rules that need to be followed while naming variables in Python.
15. Create a variable and assign any numerical value. Use the print()function to display the value stored in the variable.
Assigning a value to a variable
num = 100 # create and assign a value to the variable
print(num) # display the value of the variable
Output:
100
num =100 # create and assign a value to the variable
print(num) # display the value of the variable
num =120.5 # reassign a new value to the variable
print(num) # display the variable after re-assigning
Output:
100
120.5
16. We can assign a value of a variable to another variable. Create another variable, num2, and assign the
variable num2 to num1.
Assigning a value to a variable
num1 = 100 # assign a value to the variable num1
num2 = num1 # assign the value of num1 to the variable
num2
print("num2 :", num2) # display the variable
Output:
num2 :100
17. ● The input() function is used to get input from the user.
User input in Python
18. ● Get a string as user input and assign it to the new variable name.
User input in Python
name = input("Enter your name: ")
● Write a code to greet the user.
print("Welcome ", name)
● Click Run and enter any name.
Output:
Enter your name: Arthur
Welcome Arthur
19. Data types
Data types represent the type of data that any variable holds in its memory. We will
use the following data types in this module.
20. Data types
Let’s write a program to find the sum of 10 and any other number entered by a user.
# program to find the sum of two numbers
number1 = input("Enter the number: ")
number2 = 10
# display the sum
print("The sum is ", number1 + number2)
Output:
Feedback:
Incompatible types
You used an addition operation with a string and a number on line 4.
But you can't do that with that operator. Make sure both sides of the
operator are the right type.
21. Data types
Typecasting
Even if you enter an integer value, the input() function converts it into a string. You
need to convert it into an integer in your code using typecasting.
Convert the input to integer data type using int() function.
# program to find the sum of two numbers
number1 = int(input("Enter the number: "))
number2 = 10
# display the sum
print("The sum is ", number1 + number2)
Output:
Enter the first number: 2
The sum is 12
22. Literals are the values
which are saved in
variables/identifiers,
and they cannot be
changed.
24. Python Operators
• Operators are special symbols or keywords used to perform operations on variables and
values.
• Think of them like mathematical signs or logical tools.
Example:
a = 10
b = 5
print(a + b)
29. Arithmetic Operators
a = 10
b = 3
print("Arithmetic Operators:")
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
print("a / b =", a / b)
print("a % b =", a % b)
print("a ** b =", a ** b)
print("a // b =", a // b)
a = 10
b = 3
print("Comparison Operators:")
print("a == b:", a == b)
print("a != b:", a != b)
print("a > b:", a > b)
print("a < b:", a < b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)
Comparison Operators
Activity
30. Assignment Operators
a = 10
b = 3
print("Assignment Operators:")
x = 5
print("Initial x =", x)
x += 3
print("x += 3 →", x)
x *= 2
print("x *= 2 →", x)
x -= 4
print("x -= 4 →", x)
x /= 2
print("x /= 2 →", x)
Logical Operators
a = 10
b = 3
print(" Logical Operators:")
print("a > 5 and b < 5:", a > 5 and b < 5)
print("a < 5 or b < 5:", a < 5 or b < 5)
print("not(a > b):", not(a > b))
Activity