Python Programming Tutorial - 10: Bitwise & Logical Operators

preview_player
Показать описание
In this tutorial we'll explore two more categories of operators in python. We'll talk about the bitwise operators and the logical operators.
Рекомендации по теме
Комментарии
Автор

Thanks for the brief introduction. It was really helpful!

Zhinkk
Автор

+saikiran paladugdula You disabled replies, but here is my attempt:

import math #if i don't do this, i get an error message

#the number of sides has to be an integer:
n = int(input('Number of sides: '))
#the length of the side could be any rational number, e.g. 1.5342654:
s = float(input('Length of each side: '))

# calculate the area:
area =
# calculate the perimeter:
perimeter = n*s

# calculate funsum, and round it to 4 decimal places:
funsum = round((area + perimeter**2)*10**4)/10**4

'''convert s into an integer, but only if it's a whole number
e.g. 7.0 gets converted to 7, but 7.1 stays as it is.
If I don't do this, the print function at the end treats an integer value
as a floating point, so i get
>Funsum(5, 7.0) = 1309.3034 (4 decimal places).
'''
if s%1 == 0:
s = int(s)

#print:
print('Funsum('+str(n)+', '+str(s)+') = '+str(funsum)+' (4 decimal places).')

bszert
Автор

Hello. I tried these in Shell and all is well. However, when I try to write a very simple program it does not recognise the ^ (xor) symbol. Can you help? My program is as follows:

#We shall Xor two binary strings

print('Enter a 4-bit string p1 = ')
# For example, I enter 0b1111
p1 = input()

print('Enter a 4-bit string a1 = ')
# For example, I enter 0b1001
a1 = input()

# up to here everything is fine, but then...

print(bin(p1^a1))
# This last line of code causes an error message,
# though it works fie in 'Shell' why does it fail now?
# my thanks in advance

dd
Автор

what will be the output of
a=10
b=15
a and b

please explain...

kuljeetsingh
Автор

plz upload other topic also  as soon as

nishantjain