In: Computer Science
1. What are the main data types in Python? Which data types are sequences?
2. What are some general guidelines for naming a ‘variable’ in Python? Give at least three examples of variable names that follow these guidelines
3. Provide an example of the Python syntax to create a variable to hold the age of a building (for example,40 years old).
a. Provide the Python syntax to test whether the variable created above is more than 35, and print the test result
4. What are some of the key similarities and differences between lists and tuples in Python? Give an example, using geospatial information, of when a list would be more appropriate to use and when a tuple would be a more appropriate data type?
6. Name three methods of string objects in Python, and provide an example of each one's use.
7. Given the code below: leftovers = [‘cake’,’ice cream’] t_list = [[2.5, 1.0], ‘32’, ‘alabi’, ‘Alaska’, ‘Ohio’, ‘Armenia’, 29.05, leftovers]
a. What is the ‘data type’ of the first element in t_list?
b. What is the ‘data type’ of the second t_list element?
c. What is returned by the following code snippet? print(t_list[1])
d. What is returned by the following code snippet? print(t_list[-2])
1)Main data types in python are:-
Numbers,String,List,Dictionary,Tuples
There are 3 data types which are sequences:-
List,Tuple,Range
2) In python Variable name cannot start with number .
It should start with letter or underscore.
Variable name can contain only underscore,alphabet and numbers .
The keywords cannot be used as variables.
Example:- variable, variable9,variable_9,var_iable, _variable
3) age_building=40
a) if age_building >35 :
print(age_building)
4) Similarities in list and tuples:-
They both are sequence data types
Both are used to store multiple data at same time
Differences in ĺist and tuples:-
List is dynamic means mutable that is we can add new item or delete item
Tuples are immutable that means we cannot modify the items of tuples
Tuple is static
List is useful when we have data of dissimilar data types
Tuples are useful when we have data of similar type
6) String methods
str.join() , str.replace() , str.split()
example :-
string=" Hello Everyone"
p=( , )
p.join(string) this will show Hello, everyone
string.replace(" Hello" ,"Hi") this will display Hi everyone
string.split(( ))
This will split the string at spaces
string split(( , ))
This will split the string at comma
7)a) the 1st element[ 2.5,1.0 ] is of data type list
b) data type of second element is numbers
c) 32
d)29.05