Chapter 3: The world of variables and operators Flashcards

1
Q

What is a variable?

A

Data that we need to store and manipulate in our programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example of a variable

A

UserAge (variable) = 0 (initial value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do you have to give a variable an initial value every time it is created?

A

YEAH!!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can you change the value of a variable later in the program?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can multiple variables be defined in one go?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example of multiple variables

A

userAge, userName = 33, ‘Amrapali’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Table, chair = ‘brown’, ‘black’ is equivalent to?

A
Table = Brown
Table = Black
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What can a variable name in Python contain?

A

A variable name in Python can contain

1) Letter (a-z, A-Z)
2) Numbers
3) Underscores

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can the first character be a number?

A

No, the first character cannot be a number. variable name as 2username is invalid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of variable names

A

userAge, user_age, userAge2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What words cannot be named as usernames?

A

Words which have a preassigned meaning to them cannot be used as variable names like input and print.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is username same as userName?

A

No, because the variable names are case-sensitive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the two naming conventions for variables?

A

1) Camel case notation

2) Using underscores

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is camel case notation? Provide examples

A

Writing compound words with mixed casing. For example thisIsAVariableName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Example of underscore usage in naming a variable

A

this _is_a_variable_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is unique about ‘=’ in Python and math?

A

It has a different meaning in Python and math.

17
Q

What is the ‘=’ sign called in programming?

A

It is called the assignment operator

18
Q

What does the assignment operator mean?

A

It means we are assigning the value on the right side of the = sign to the variable on the left

19
Q
If you type the below code into your IDLE editor
X= 5
Y= 10
X=Y
print ("X=", X)
print ("Y=", Y)

What will be the output? Explain.

A

X= 10
Y= 10
Because, the third line assigns Y’s value to X

20
Q

What are the basic operators in Python?

A

Addition, subtraction, multiplication, division, floor division, modulus and exponent

21
Q

Provide examples of all the basic operators

A

x=5
y=2
Addition:
x+y=7

Subtraction:
x-y=3

Multiplication:
x*y= 10

Division:
x/y= 2.5

Floor Division:
x//y=2 (it rounds down the answer to the nearest whole number)

Modulus:
x%y=1 (gives the remainder when 5 is divided by 2)

Exponent:
x**y=25 (5 to the power 2)

22
Q

What are the other assignment operators beside “=’?

A

+=, -= and *=

23
Q

What does += implies?

A

If x= 10 then x+=2 i.e. x= x+2

the output will be x= 12

24
Q

What does x-= implies?

A

If x= 10 then x-=2 i.e. x=x-2

the output will be x= 8

25
Q
What will be the output of below code?
x=10
x+=2
print(x)
x-=2
print(x)
A

For the first print(x) will be 12 and the next one 10. This is because post executing the first print command x now has the value of 12 and the next -= function actually subtracts 2 from 12 thus giving us 10. This is an example of the fact that computer executes sequentially. If you want the subtraction answer as 8 you need to define the variable again.