CTP - Introduction To Python Basics - Lect005
CTP - Introduction To Python Basics - Lect005
Saurabh Singh
Department of AI & Big Data
Woosong University
2024/10/16
Python Assignment
Operators
• Assignment operators are used to assign values to variables:
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
|= x |= 3 x=x|3
Python Comparison Operators
• Comparison operators are used to compare two values:
a = 10
b = 10
c = -10
if a > 0 and b > 0:
print("The numbers are greater than 0")
if a > 0 and b > 0 and c > 0:
print("The numbers are greater than 0")
else:
print("Atleast one number is not greater than 0")
Python OR Operator
a = 10
b = -10
c=0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
a = 10
b = 12
c=0
if a or b or c:
print("Atleast one number has boolean value as True")
else:
print("All the numbers have boolean value as False")
Python NOT Operator
• The Boolean NOT operator works with a single boolean value. If the
boolean value is True it returns False and vice-versa.
a = 10
if not a:
print("Boolean value of a is True")
if not (a % 3 == 0 or a % 5 == 0):
print("10 is not divisible by either 3 or 5")
else:
print("10 is divisible by either 3 or 5")
Exercise
• Logical Operators With Boolean Conditions
x = 10
y = 20
print("x > 0 and x < 10:",x > 0 and x < 10)
print("x > 0 and y > 10:",x > 0 and y > 10)
print("x > 10 or y > 10:",x > 10 or y > 10)
print("x%2 == 0 and y%2 == 0:",x%2 == 0 and y%2 == 0)
print ("not (x+y>15):", not (x+y)>15) x > 0 and x < 10: False
x > 0 and y > 10: True
x > 10 or y > 10: True
x%2 == 0 and y%2 == 0: True
not (x+y>15): False
Boolean Expressions
• A boolean expression is an expression that is either true or false. The following examples
use the operator ==, which compares two operands and produces True if they are equal
and False otherwise:
>>>5 == 5
>>>True
>>> 5 == 6
>>>False
• True and False are special values that belong to the class bool; they are not strings:
• >> type(True)
• <class 'bool'>
• >>> type(False)
• <class 'bool'>
Boolean Expressions
• The == operator is one of the comparison operators; the others are:
• x != y # x is not equal to y
• x>y # x is greater than y
• x<y # x is less than y
• x >= y # x is greater than or equal to y
• x <= y # x is less than or equal to y
• x is y # x is the same as y
• x is not y # x is not the same as y
• A common error is to use a single equal sign (=) instead of a double equal sign (==).
• Remember that = is an assignment operator and == is a comparison operator.
• There is no such thing as =< or =>.
Asking the user for input
• Python interpreter works in interactive and scripted mode. While the
interactive mode is good for quick evaluations, it is less productive.
For repeated execution of same set of instructions, scripted mode
should be used.
• Let us write a simple Python script to start with.
>>>name = "Kiran"
The program simply prints the values of the two
>>>city = "Hyderabad" variables in it. If you run the program repeatedly,
the same output will be displayed every time. To
>>>print ("Hello My name is", name) use the program for another name and city, you
can edit the code, change name to say "Ravi"
>>>print ("I am from", city) and city to "Chennai". Every time you need to
assign different value, you will have to edit the
program, save and run, which is not the ideal
way.
Asking the user for input
• The input() Function: you need some mechanism to assign different value to the variable in
the runtime − while the program is running. Python's input() function does the same job.
name = input()
city = input()
print ("Hello My name is", name)
print ("I am from ", city)
• Sometimes we would like to take the value for a variable from the user via their keyboard.
• Python provides a built-in function called input that gets input from the keyboard.
• When this function is called, the program stops and waits for the user to type something.
• When the user presses Return or Enter, the program resumes and input returns what the
user typed as a string.
Asking the user for input
• Before getting input from the user, it is a good idea to print a prompt telling the user what to
input.
• You can pass a string to input to be displayed to the user before pausing for input:
• >>> name = input('What is your name?\n')
• What is your name?
• Chuck
• >>> print(name)
• Chuck
• The sequence \n at the end of the prompt represents a newline, which is a special character
that causes a line break. That’s why the user’s input appears below the prompt.
• If you expect the user to type an integer, you can try to convert the return value to int using
the int() function:
Asking the user for input
name = input("Enter your name : ")
city = input("Enter your city : ")
print ("Hello My name is", name)
print ("I am from ", city)
If you expect the user to type an integer, you can try to convert the return value to int using
the int() function:
name = input("Enter your name : ")
city = input("Enter your city : ")
Write it by urself… #age = input("Enter your age: ")
Name= age = int(input("Enter your age: "))
City= print ("Hello My name is", name)
Age= print ("I am from ", city)
print(age)
• Taking Numeric Input in Python
width = input("Enter width : ")
height = input("Enter height : ")
area = width*height
print ("Area of rectangle = ", area)
• hours = 35.0
• rate = 12.50
• pay = hours * rate
• print(pay)
• x1q3z9ahd = 35.0
• x1q3z9afd = 12.50
• x1q3p9afd = x1q3z9ahd * x1q3z9afd
• print(x1q3p9afd)
Choosing mnemonic variable names
• The Python interpreter sees all three of these programs as exactly the
same but humans see and understand these programs quite
differently.
• Humans will most quickly understand the intent of the second
program because the programmer has chosen variable names that
reflect their intent regarding what data will be stored in each variable.
• We call these wisely chosen variable names “mnemonic variable
names”. The word mnemonic means “memory aid”. We choose
mnemonic variable names to help us remember why we created the
variable in the first place.
Choosing mnemonic variable names
• for word in words:
• print(word)
• It is easier for the beginning programmer to look at this code and know which
parts are reserved words defined by Python and which parts are simply variable
names chosen by the programmer.
• After a pretty short period of time, you will know the most common reserved
words and you will start to see the reserved words jumping out at you:
Conditional Execution….
• Thank you