sa 12 cs chapter 2

7. Define two variables first and second so that first = “Jimmy” and second = “Johny”. Write a short Python code segment that swaps the values assigned to these two variables and prints the results.

# Code first = ‘Jimmy’ second = ‘Johny’ print(first, second) # Jimmy Johny first, second = second, first # swaps the variables print(first, second) # Johny Jimmy

7. Define two variables first and second so that first = “Jimmy” and second = “Johny”. Write a short Python code segment that swaps the values assigned to these two variables and prints the results. Read More »

1. Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input. Display if the phone number entered is valid format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific places).

# Code p = input(‘Enter Phone Number : ‘) val = False # length must be 12 if len(p) == 12 and p[3] == ‘-‘ and p[7] == ‘-‘: if (p[:3]+p[4:7]+p[8:]).isdigit(): # all the characters except dashes should be digits val = True if val:     print(p, ‘is valid’) else:     print(p, ‘is invalid’)

1. Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input. Display if the phone number entered is valid format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific places). Read More »