In: Computer Science
Using Python
In this assignment we will try to add tuples, lists, if statements and strings to our program. For example, you can ask for a user for a couple items, their name and age – and depending on their age, print out a predefined list. You could ask for some string input and decide to do something with the output. You could ask for three items, 1) age, 2) are you a male or female and 3) are your in school do you work, or both?
Do not concern yourself with the actual output, but depending on what a user gives you, you can print out different outputs, even a Tuple if you like.
Take a snapshot of your project, the source code and the result - was it successful, did you have any issues, did you make any mistakes and learn, etc.
Strings in Python
A string is a sequence of characters. It can be declared in python
by using double quotes. Strings are immutable, i.e., they cannot be
changed.
# Assigning string to a variable
a = "This is a string"
print a
OUTPUT:-
This is a string
Lists in Python
Lists are one of the most powerful tools in python. They are just
like the arrays declared in other languages. But the most powerful
thing is that list need not be always homogenous. A single list can
contain strings, integers, as well as objects. Lists can also be
used for implementing stacks and queues. Lists are mutable, i.e.,
they can be altered once declared.
# Declaring a list
L = [1, "a" , "string" , 1+2]
print L
L.append(6)
print L
L.pop()
print L
print L[1]
OUTPUT:-
[1, 'a', 'string', 3] [1, 'a', 'string', 3, 6] [1, 'a', 'string', 3] a
Tuples in Python
A tuple is a sequence of immutable Python objects. Tuples are just
like lists with the exception that tuples cannot be changed once
declared. Tuples are usually faster than lists.
tup = (1, "a", "string", 1+2)
print tup
print tup[1]
OUTPUT:-
(1, 'a', 'string', 3) a
If else statement in python :
# If the number is positive, we print an appropriate message
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
OUTPUT: -
3 is a positive number This is always printed This is also always printed.