give a grammar and example of if statement for each of the following languages: a)perl, b)python

Recommended Answers

All 11 Replies

python = 77777
if python:
    print("You picked a good language")

I like this one:

w, x, y, z = 1, 2, 3, 4

if w < x < y < z:
    print("w={} x={}  y={}  z={}".format(w, x, y, z))

In case you don't like multiple if/elif/else statements:

''' switch_case.py
a switch/case like statement to replace
multiple if/elif/else statements
'''

# make input() work with Python2 and 3
try:
    input = raw_input
except:
    pass

def switch_case(case):
    return "You entered " + {
    '1': "one",
    '2': "two",
    '3': "three",
    '4': "four"
    }.get(case, "a bad number")

num = input("Input a number between 1 and 4: ")
print(switch_case(num))
a = int(input("Enter a number: ")) # that is the function for entering an integer from keyboard

if a == 0:
    print("It is 0")
elif a < 0:
    print("It is negative")
else:
    print("It is positive")

#you can also use other operators
if a > 5:
    print("a is bigger than 5")

if a < 11:
    print("a is smaller than 11")

if a > 5 & a < 100:
    print("a is bigger than 5 and smaller than 100")

if a > 50 | a > 100:
    print("a might be bigger than 50 or might be bigger than 100")

# and so n...
#hope that i was helping you

Python allows conditional expression

if y == 1:
    x = 5
else:
    x = 3

can written as

x = 5 if y == 1 else 3

More examples:

# a multiconditional statement
if x > 0:
  if x < 10:
    print("x is larger than zero and less than 10")

# a simpler version
if 0 < x < 10:
  print("x is larger than zero and less than 10")

# is character lower case?
if 'a' <= character <= 'z':
  print(character)
# test string
mystr = "Zero Animal zones."

for c in mystr:
    if 'a' <= c <= 'z':
        print("character {} is between 'a' and 'z'".format(c))
    elif 'A' <= c <= 'Z':
        print("character {} is between 'A' and 'Z'".format(c))
    else:
        print(c, ord(c))
on = True
off = False
b1 = on
b2 = on
b3 = on

if b1 == b2 == b3:
    print("all buttons on")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.