5.1.3 Tools & code analysis: Analyzing Scripts in Python Flashcards

1
Q

Coding in Python: how comments are written in Python?

A

▪ # This is the first line of my script
▪ # This script is used to do backups of my systems

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

Coding in Python: explain a variable in Python and how to spot it?

A

A variable is a name that refers to a value. It’s like a label for a piece of data, such as a number, a string of text, or a more complex object. You can think of a variable as a container that holds information. To spot a variable in Python, look for a name followed by an equal sign and a value assignment. For example:
x = 5
name = “Alice”

In this code, “x” and “name” are variables. They are being assigned the values 5 and “Alice” respectively. You can use these variables later in your code to work with the values they store.

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

Coding in Python: explain an array in Python and how to spot it?

A

An array is a data structure that can hold a fixed number of elements of the same type. It is used to store collections of data. However, in Python, the list type is more commonly used to store arrays of elements. To spot a list (array) in Python, look for square brackets enclosing a comma-separated list of elements. For example:
my_list = [1, 2, 3, 4, 5]

In this code, my_list is a list (array) containing the elements 1, 2, 3, 4, and 5. You can access individual elements within the list using their index, and you can perform various operations on the list, such as adding or removing elements.

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

Coding in Python: what is the difference between an array and a variable?

A

A variable in Python is a name that refers to a single value. It’s like a label for a specific piece of data, such as a number, a string of text, or a more complex object. You can think of a variable as a container that holds a single piece of information.

On the other hand, an array in Python (usually implemented using a list) is a data structure that can hold a collection of elements, all of the same type. It’s used to store multiple values in a single variable. You can access each element in the array using its index.

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

Coding in Python: explain Associative Arrays in Python

A

Associative arrays are implemented using dictionaries. A dictionary is a collection of key-value pairs, where each key is unique and maps to a specific value. To create an associative array in Python, you use curly brackets {} and specify key-value pairs within them, separated by colons. For example:
my_dict = {“name”: “Alice”, “age”: 25, “city”: “New York”}

In this example, my_dict is an associative array (dictionary) where “name”, “age”, and “city” are keys, and “Alice”, 25, and “New York” are their respective values.

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

Coding in Python: explain Named Arrays in Python

A

There is no named array in Python

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

Coding in Python: provide the basic writing for arithmetic in Python (=, !=,<,> etc)

A

a and b are variables
▪ is equal to: a == b
▪ is not equal to: a != b OR a <> b
▪ is greater than: a > b
▪ is greater than or equal to: a >= b
▪ is less than: a < b
▪ is less than or equal to: a <= b

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

Coding in Python: explain conditional statement in Python (3)

A

1/ The “if” statement is used to execute a block of code if a specified condition is true. For example:
x = 10
if x > 5:
print(“x is greater than 5”)

2/ The “elif” statement is used to check additional conditions if the previous conditions were not true. For example:
x = 10
if x > 5:
print(“x is greater than 5”)
elif x == 5:
print(“x is equal to 5”)

3/ The “else” statement is used to execute a block of code if none of the previous conditions are true. For example:
x = 10
if x > 5:
print(“x is greater than 5”)
elif x == 5:
print(“x is equal to 5”)
else:
print(“x is less than 5”)
In this code, if neither “x > 5” nor “x == 5” is true, it will execute the print statement in the “else” block

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

Coding in Python: explain loops in Python (2)

A

In Python, the “for” and “while” loops are used to execute a block of code repeatedly.

1/ The “for” loop is used when you know in advance how many times you want to execute a block of code. Example:
for i in range(5):
print(i)

2/ The “while” loop is used when you want to execute a block of code as long as a specified condition is true. It repeatedly executes the block of code until the condition becomes false. For example:
x = 0
while x < 5:
print(x)
x += 1
In this code, the “while” loop repeatedly prints the value of “x” as long as “x” is less than 5, and then increments “x” by 1 in each iteration.

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

Coding in Python: how to input data with python?

A

In Python, you can input data from the user using the input() function. When input() is called, the program waits for the user to enter some text followed by the “Enter” key. The text entered by the user is then returned as a string. Example:
name = input(“Enter your name: “)
print(“Hello, “ + name + “!”)

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

Coding in Python: explain this tempFile = open(‘test.txt’, ‘w’)

A

The line of code tempFile = open(‘test.txt’, ‘w’) in Python opens a file named “test.txt” in write mode and assigns the file object to the variable tempFile.

The ‘w’ mode indicates that the file is being opened for writing. If the file does not exist, a new file is created. If the file does exist, its previous contents are erased.

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

Coding in Python: explain this tempFile = open(‘test.txt’, ‘a’)

A

The line of code tempFile = open(‘test.txt’, ‘a’) in Python opens a file named “test.txt” in append mode (= adding new data to the end of an existing file) and assigns the file object to the variable tempFile.

The ‘a’ mode indicates that the file is being opened for appending. If the file already exists, the new data will be written to the end of the file. If the file does not exist, it will be created.

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