Variables and Data Types

 

Topic 1: Question 1

Assign the value 5 to a, and value 6 to b. Assign the value of a + b to variable c.

a = 5
b = 6
c = a + b

 

Topic 1: Question 2

 It is important to take note of the difference between adding numbers and 'adding' string. 5 + 5 will not produce the same result as '5' + '5'.

[MCQ] Which of the following will produce the right result?

2 + 5 produces '25'
'2' + 5 produces '25'
'2' + '5' produces '25'
2 + '5' produces '25'

 

Topic 1: Question 3

Floating point number is represented with a dot(.) followed by one or more decimals (can be zero).

#Compute the area and perimeter of a circle with radius = 3
pi = 3.14
radius = 3
area = pi * radius**2
perimeter = pi * 2 * 3

 

Topic 1: Question 4

Numbers that contains decimal point are called floating point numbers. The type(x) function will return <type 'float'=""> if the argument x is a float. You can use the float(x) and int(x) function to convert values between float and integer.                            

# Change the type of the variable x to float
# Change the type of variable y to integer
x = 123446754336788543835697
y = 3.14159265358979323846
x = float(x)
y = int (y)

 

Topic 1: Question 5

Declare a string literal by enclosing the literal using single, double or triple quotes. Triple quote allows the literal to span multiple lines.

# Assign foobar which gives the output shown in the last example.
# Hint: Use the triple quote as the outermost quote
foobar ='\"No, thanks, Mom,\" I said, \"I don\'t know how long it will take.\"'

 

Topic 1: Question 6

Certain mathematical operations such as addition and multiplication can be used on the String datatype. Study the examples given below to see how it works:

# Assign 'HelloWorld!' to variable a
a = 'Hello'+'World!'

# b contains 'HelloWorld!HelloWorld!HelloWorld!HelloWorld!HelloWorld!'
b = 'HelloWorld!' * 5

 

Topic 1: Question 7

You can make use of the len(x) function to find out the number of characters in a string. 

greeting = "Hello Google!"
# number of characters stored in the variable greeting
number_of_char =len(greeting)

# repeat the greetings based on the number of character in 'greeting'
greetings =greeting * len(greeting)

 

Topic 1: Question 8

Adding two strings or making multiple copies of the same string.

 Examples

    >>> greetings = "Hello World" >>> len(greetings) # get the length of string 11 >>> greetings[0] # get the 1st character 'H' >>> "@@" + "@@@" # add strings or characters using '+' operator '@@@@@' >>> "@" * 5 # make mutliple copies using '*' operator '@@@@@' >>> print underline("Good Day") Good Day ________ >>> 
def underline(title):
return title + '\n' + len(title) * '_'

 

Topic 1: Question 9

 Introducing some string methods.

# Use one or more string methods in above examples, extract the substring
# surrounded by 'xyz' at the beginning and end. Replace the ',' in the substring with '|'.
# and remove all trailing space.

str1 = 'abcefghxyzThis,is,the,target,string xyzlkdjf'
idx1 = str1.find('xyz') # get the position of 'xyz'
idx2 = str1.find('xyz', idx1+1) # get the next 'xyz'
str1 = str1[idx1+3:idx2].replace(',','|') # replace ',' with '|'
str1 = str1.strip() # strip trailing spaces.

 

Topic 1: Question 10

Like other programming languages, Python also has some basic types like numbers, strings, lists and dictionaries.

# Assign arbitrary values to the variables such that they are of the types used in the examples
a = '11'
b = 11
c = 11.01
d = [1,2,3,4,5]

 

Topic 1: Question 11

 There are some rules in the naming of variables.

[MCQ] Which of the following are not valid variable names in Python?
a: _hello
b: $hello
c: hello
d: hello world

a and b
a only
d only
a and c
b and d

 

Topic 1: Question 12

 A integer or floating-point number with trailing 'j' or 'J' is a complex number.

# Compute the sum and product of 2 complex numbers:
# (2+3j) and (4+5j)
a = 2+3j
b = 4+5j
sum_ab = a + b
prod_ab = a * b

 

Topic 1: Question 13

 Format string output by using the '%' operator

# Write a function that does a decimal to hexadecimal conversion.
# Hint: Make use of "%x" for hexadecimal format.
def dec2hex(num):
  return str('0x' + '%.2x' % num)

 

Topic 1: Question 14

   Accessing string elements.

    A string is a sequence of characters.
    Each character can be retrieved using an integer index, starting from zero.
    To access a substring use s[i:j], which returns a substring from index i to
    index (j-1).
# Extract each word from 'greetings' and assign to 
# variables 'first', 'middle' and 'last'.
greetings = "How are you"
first = greetings[ 0:3]
middle = greetings[ 4: 7]
last = greetings[ 8:11 ]

 

Topic 1: Question 15

   Octal and hexadecimal integer.

    The default literal representation is in decimal format. To represent a octal or hexadecimal literal,
    precede the value with '0' and '0x' respectively.
# Assign the value of 25 using decimal, octal and hexadecimal to a, b, and c respectively.

a = 25
b = 031
c = 0x19

 

Topic 1: Question 16

Question:
[MCQ] What are the final values of x and y at the end of execution?
          def changeval(x):
                global y
                x = 3
                y = 4

          x = 1
          y = 2

          changeval(x)

          print x
          print y

x = 1, y = 2
x = 3, y = 4
x = 3, y = 2
x = 1, y = 4
None of the above.   

 

Topic 1: Question 17

 Python supports several data types. The commonly used ones are int, str, float, list, tuple and dictionary.

Question:
[MCQ] What will be printed by the code given below?
>>> type(5/2)

<type 'int'>
<type 'float'>
<type 'list'>
<type 'str'>
<type 'tuple'>

Topic 1: Question 18

 Python supports the following logical operators: and, or and not. These operators can be chained to test for more than one conditions.   

Question:
[MCQ] What is the value of x?
x = 1 == 1 and 1 != 0 or 1 > 0

-1
0
True
False

URL: http://www.pyschools.com/quiz/view_topic/s1

你可能感兴趣的:(Variables and Data Types)