0% found this document useful (0 votes)
4 views24 pages

CTP - Introduction To Python Basics - Lect005

Learn python easily!

Uploaded by

101lightyagami94
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views24 pages

CTP - Introduction To Python Basics - Lect005

Learn python easily!

Uploaded by

101lightyagami94
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 24

Computational thinking and Programming

(Introduction to basic Python)

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

|= x |= 3 x=x|3
Python Comparison Operators
• Comparison operators are used to compare two values:

Operator Name Example


== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal x >= y
to
<= Less than or equal to x <= y
Python Logical Operators

• Logical operators are used to combine conditional statements:

Operator Description Example


and Returns True if both x < 5 and x < 10
statements are true

a = 10 or Returns True if one of x < 5 or x < 4


the statements is true
b = 10
c = -10 not Reverse the result, not(x < 5 and x <
if a > 0 and b > 0: returns False if the 10)
print("The numbers are greater than 0") result is true
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")
Logical Operators
• There are three logical operators: and, or, and not.
• The semantics (meaning) of these operators is similar to their meaning in English.
• n%2 == 0 or n%3 == 0 is true if either of the conditions is true, that is, if the number is
divisible by 2 or 3.
• Finally, the not operator negates a boolean expression, so not (x > y) is true if x > y is
false; that is, if x is less than or equal to y.
• Strictly speaking, the operands of the logical operators should be boolean expressions,
but Python is not very strict. Any nonzero number is interpreted as “true.
>>> 17 and True
>>>True
• This flexibility can be useful, but there are some subtleties to it that might be
confusing. You might want to avoid it until you are sure you know what you are doing.
AND Operator in Python

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)

Error encountered : TypeError: can't multiply sequence by non-int of type


'str‘
• Why do you get a TypeError here? The reason is, Python always read the
user input as a string. Hence, width="20" and height="30" are the strings
and obviously you cannot perform multiplication of two strings
• To overcome this problem, we shall use int(), another built-in function
from Python's standard library. It converts a string object to an integer.
• Practice
width = int(input("Enter width : "))
height = int(input("Enter height : "))
area = width*height
print ("Area of rectangle = ", area)

Likewise, Python's float() function converts a string into a float object.


Practice:
amount = float(input("Enter Amount : "))
rate = float(input("Enter rate of interest : "))
interest = amount*rate/100
print ("Amount: ", amount, "Interest: ", interest)
The print() Function
• Python's print() function is a built-in function. It is the most frequently used
function, that displays value of Python expression given in parenthesis
• If there are multiple comma separated objects in the print() function's
parenthesis, the values are separated by a white space " ".
• To use any other character as a separator, define a sep parameter for the
print() function.
city="Hyderabad"
state="Telangana“
country="India"
print(city,state,country,sep=',')
• Practice:
a = "Hello World"
b = 100
c = 25.50
d = 5+6j
print ("Message:”, a)
print (b, c, b-c)
print(pow(100, 0.5), pow(c,2))
Choosing mnemonic variable names
• As long as you follow the simple rules of variable naming, and avoid
reserved words, you have a lot of choice when you name your
variables.
• In the beginning, this choice can be confusing both when you read a
program and when you write your own programs.
• For example, the following three programs are identical in terms of
what they accomplish, but very different when you read them and try
to understand them.
Choosing mnemonic variable names
• a = 35.0
• b = 12.50
• c=a*b
• print(c)

• 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)

• for slice in pizza:


• print(slice)

• 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

You might also like