Course Name: Programming for Big Data
Course Code: CSDS-4423
Credit Hours: 3
Instructor: Prof. Khalid Rasheed Ch.
Lecture #: 3
Today Agenda
Keywords
Variable & expression
Operators
Selection structure
Keywords
In Python, keywords are reserved words that have special meanings and are
part of the language syntax. These keywords cannot be used as identifiers (such
as variable names or function names) because they are used to define the
structure and flow of the code. Here is a list of Python
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Variable
Variables are used to store data values. They provide a way to label and reference memory locations in a
program. In Python, variables are created when you assign a value to them using the assignment
operator (=). Variables can hold various types of data such as numbers, strings, lists, dictionaries, etc.
Rules of Writing variable
Valid Characters: Variable names can contain letters (both uppercase and lowercase), digits, and
underscores (_). They must start with a letter or an underscore, but they cannot start with a digit.
Case-Sensitive: Python variable names are case-sensitive, meaning my_variable, My_Variable, and
MY_VARIABLE are all different variables
Reserved Keywords: You cannot use Python keywords as variable names. Keywords are reserved for
specific purposes and have predefined meanings in the language. Attempting to use a keyword as a
variable name will result in a syntax error.
No Special Characters: Variable names cannot contain special characters such as !, @, $, %, ^, &, *, etc.,
except for the underscore (_).
Meaningful and Descriptive: Choose variable names that are meaningful and descriptive of the data they
represent. This enhances code readability and makes it easier for others (and your future self) to
understand the code.
Hello World! In Python
print("Hello, World!")
Input method in python
name = input("What is your name? ")
print("Hello, " + name + "! Welcome to Python.")
Program Example # 1
# Take two inputs from the user
input1 = input("Enter the first input: ")
input2 = input("Enter the second input: ")
# Display the inputs
print("First input:", input1)
print("Second input:", input2)
Program Example # 2
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print("The sum of", num1, "and", num2, "is:", sum)
Data types in Python
1.(int): Whole numbers without decimal points, e.g., 5, -10, 1000
2.(float): Numbers with decimal points, e.g., 3.14, -0.001, 2.0.
3.Strings (str): Ordered sequence of characters, enclosed within single quotes (' '),
double quotes (" "), or triple quotes (''' ''' or """ """), e.g., 'hello', "world",
'''Python'‘’.
4.Booleans (bool): Represents the truth values True and False, used in logical
operations and comparisons.
5.Lists (list): Ordered collection of items, mutable (can be changed), enclosed
within square brackets [], e.g., [1, 2, 3], ['apple', 'banana', 'orange’].
6.Tuples (tuple): Similar to lists but immutable (cannot be changed), enclosed
within parentheses (), e.g., (1, 2, 3), ('a', 'b', 'c’).
7.Dictionaries (dict): Unordered collection of key-value pairs, enclosed within
curly braces {}, e.g., {'name': 'John', 'age': 30, 'city': 'New York’}.
8.Sets (set): Unordered collection of unique items, enclosed within curly braces {},
e.g., {1, 2, 3}, {'apple', 'banana', 'orange’}.
9.NoneType (None): Represents the absence of a value or a null value.
Arithmetic operators in python
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Logical Operators
And Operator Not operator
a=int(input("enter the value"))
a=int(input("enter the value"))
b=int(input("enter the value"))
b=int(input("enter the value"))
if not a>b:
c=int(input("enter the value"))
print("All is well")
if a > b and a > c:
print("a is greater than b and c")
Or Operator
a=int(input("enter the value"))
b=int(input("enter the value"))
c=int(input("enter the value"))
if a > b or a > c:
print("a is greater than b or c")
Selection structure in Python
If statement If else
a=int(input("enter the value")) a=int(input("enter the value"))
if a >=40: if a >=40:
print("pass") print("pass")
else:
print("fail")
Nested if If Else if
a=int(input("enter the value")) a=int(input("enter the value"))
b=int(input("enter the value")) if a > 0:
c=int(input("enter the value")) print("positive")
if a > b: elseif a< 0:
if a > c: print("negative")
print("a is greater than b and c") else: print("zero")
Loops in python
While Loop For loop Nested Loop
Example 1: for i in range(1,10): for i in range(1, 6):
a=1 print(i) for j in range(1, i + 1):
while a<=10: print("*",end="")
print(a) for i in range(1,10,2): print()
a+=1 print(i)
Example 2
Example 2: Example 2: for i in range(5, 0,-1):
for j in range(1, i + 1):
a=int(input("enter a number")) a=int(input("enter a number")) print("*",end="")
b=1 for i in range(1,11): print()
while b<=10: print(a,"X",i,"=",a*i )
print(a,"X",b,"=",a*b )
b+=1
More Examples Factorial of given number
i=1 a=int(input("enter a number"))
sum=0 f=1
while i<=5: c=1
print(i) while c <= a:
sum=sum+i f= f * c
i+=1 c+=1
print("sum is = ",sum) print("the factorial is =", f)
_________________________
sum=0
for i in range(1,6):
sum=sum+i
print(i)
print("sum is = ",sum)
_________________________
Output
1
2
3
4
5
Sum is = 15