Chapter 2

Why do we need to use variables in code?

Variables work as aliases or symbols in mathematic equations. With variables, we can write business logic, or how, without knowing specific values, or what, beforehand – we don't have to repeat doing so over and over again.

What is the recommended way of naming variables? Why does it matter?

There are a few simple requirements when it comes to naming variables that are mandatory—they can't start with a number, contain whitespaces, or special characters. Finally, none of the keywords that are reserved by Python can be used.

That being said, there is some guidance on to better naming; first of all – PEP8. According to PEP, it is recommended to name variables meaningfully and consistently so that they are easy to understand. It is also suggested to use "snakecase" (lowercase whitespace represented by an underscore) for functions and variables and "camelCase" (words joined together, with second and further words beginning with an uppercase letter). None of this execution, but it will help you navigate the code.

What do data types mean and why do they matter for computation?

Data types define core properties of values—how much memory to allocate for them, what the corresponding methods are, and how other objects interact with them.

What are the four most popular data types in Python?

The most popular (common) data types are string, integer, float, and Boolean.

What does the @ operator stand for? Why doesn't it work?

Starting with Python 3.5, it is reserved for matrix multiplication, but it is not implemented (neither are matrices) in core Python.

What are the two operators that will work with strings?

Addition (+) will concatenate two strings together; for example:

>>> 'Good ' + 'Morning'
'Good Morning'

Multiplication by an integer (*) will repeat a string a corresponding number of times; for example:

>>> 'Repeat'*3
'RepeatRepeatRepeat'

How would you combine the results of two tests if we need to return a True value, but only when both of them return True? What about when at least one returns True? What about if only one (but not both) returns True?

This can be done using the AND, OR, and XOR operators. Take a look:

>>> (1 > 0) and (0 > -1)
True
>>> (1 > 0) or (0 > 1)
True
>>> (1 > 0) ^ (0 > -1)
False

Simple!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.94.193