Variables Flashcards

1
Q

Variables

A

Variables are containers for storing data values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
x = 5
y = "John"
A

print(x)
print(y)

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

Casting a variable

A

If you want to specify the data type of a variable, this can be done with casting.

x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Get the Type of varibale

A

Get the data type of a variable with the type() function.

x = 5
y = “John”
print(type(x))
print(type(y))

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

String variables can be declared either by using single or double quotes

A
x = "John"
# is the same as
x = 'John'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Case-Sensitive variables

A

Variable names are case-sensitive.

a = 4
A = "Sally"
#A will not overwrite a
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Rules for Python variables:

A
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Variable name examples

A
  • myvar = “John”
  • my_var = “John”
  • _my_var = “John”
  • myVar = “John”
  • MYVAR = “John”
  • myvar2 = “John”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Illegal variable names

A
  • 2myvar = “John”
  • my-var = “John”
  • my var = “John”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Camel Case variable name

A

Each word, except the first, starts with a capital letter:

myVariableName = “John”

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

Pascal Case variable name

A

Each word starts with a capital letter:

MyVariableName = “John”

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

Snake Case

A

my_variable_name = “John”

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

Assign values to multiple variables in one line:

A

x, y, z = “Orange”, “Banana”, “Cherry”

  • print(x)
  • print(y)
  • print(z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assign the same value to multiple variables in one line:

A

x = y = z = “Orange”

  • print(x)
  • print(y)
  • print(z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Unpacking variables is:

A

If you have a collection of values in a list, tuple etc. Python allows you extract the values into variables.

fruits = [“apple”, “banana”, “cherry”]

x, y, z = fruits

  • print(x)
  • print(y)
  • print(z)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

This is:

fruits = [“apple”, “banana”, “cherry”]
x, y, z = fruits
print(x)
print(y)
print(z)

A

Unpacking variables

17
Q

Output variables

A

print() statement is often used to output variables.

x = “awesome”
print(“Python is “ + x)

18
Q

add a variable to another variable:

A

+ character to add a variable to another variable:

x = “Python is “
y = “awesome”
z = x + y
print(z)

19
Q

For numbers, the + character works as a:

A

Mathematical operator

x = 5
y = 10
print(x + y)

20
Q

If you try to combine a string and a number:

A

Python will give you an error:

x = 5
y = “John”
print(x + y)

21
Q

Can you combine a string and a number?

A

No, Python will give you an error.

x = 5
y = “John”
print(x + y)

22
Q

Global Variables

A

Variables that are created outside of a function.

x = "awesome"
def myfunc( ):
 print("Python is " + x)
myfunc( )
23
Q

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

A

x = “awesome”

def myfunc():
 x = "fantastic"
 print("Python is " + x)

myfunc()

print(“Python is “ + x)

24
Q

Global Keyword

A

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.

To create a global variable inside a function, you can use the global keyword.

def myfunc():
 global x
 x = "fantastic"
myfunc()
print("Python is " + x)
25
Q

Change a global variable inside a function

A

Use the global keyword

x = "awesome"
def myfunc():
 global x
 x = "fantastic"
myfunc()
print("Python is " + x)