Python Notes
Python Notes
high-level language
The IL is, in fact, the alphabet of a machine language. This is the simplest and most primary
set of symbols we can use to give commands to a computer. It's the computer's mother tongue.
Unfortunately, this mother tongue is a far cry from a human mother tongue. We both (computers
and humans) need something else, a common language for computers and humans, or a bridge
between the two different worlds.
We need a language in which humans can write their programs and a language that computers
may use to execute the programs, one that is far more complex than machine language and yet
far simpler than natural language.
Such languages are often called high-level programming languages. They are at least somewhat
similar to natural ones in that they use symbols, words and conventions readable to humans.
These languages enable humans to express commands to computers that are much more complex
than those offered by ILs.
A program written in a high-level programming language is called a source code (in contrast to
the machine code executed by computers). Similarly, the file containing the source code is called
the source file.
As you can see, this first program consists of the following parts:
Scenario
The print() command, which is one of the easiest directives in Python, simply prints out a line to
the screen.
Use the print() function to print the line Hello, Python! to the screen. Use double quotes
around the string.
Having done that, use the print() function again, but this time print your first name.
Remove the double quotes and run your code. Watch Python's reaction. What kind of
error is thrown?
Then, remove the parentheses, put back the double quotes, and run your code again. What
kind of error is thrown this time?
Experiment as much as you can. Change double quotes to single quotes, use
multiple print() functions on the same line, and then on different lines. See what
happens.
print()
Positional arguments
print("My name is", "Python.")
print("Monty Python.")
Keyword arguments
print("My name is", "Python.", end=" ")
print("Monty Python.")
print("My", "name", "is", "Monty", "Python.", sep="*")
print("My", "name", "is", sep="_", end="*")
print("Monty", "Python.", sep="*", end="*\n")
Literals – the data in itself
A literal is data whose values are determined by the literal itself.
A // (double slash) sign is an integer division operator. It differs from the standard / operator in
two details:
its result lacks the fractional part ‒ it's absent (for integers), or is always equal to zero (for
floats); this means that the results are always rounded;
it conforms to the integer vs. float rule.
print(6 // 3)
print(6 // 3.)
print(6. // 3)
print(6. // 3.)
Remainder (modulo)
The next operator is quite a peculiar one, because it has no equivalent among traditional
arithmetic operators.
Its graphical representation in Python is the % (percent) sign, which may look a bit confusing.
Try to think of it as a slash (division operator) accompanied by two funny little circles.
The result of the operator is a remainder left after the integer division.
In other words, it's the value left over after dividing one value by another to produce an integer
quotient.
print(14 % 4)
Variables
A variable comes into existence as a result of assigning a value to it. Unlike in other
languages, you don't need to declare it in any special way.
If you assign any value to a nonexistent variable, the variable will be automatically created.
You don't need to do anything else.
The creation (in other words, its syntax) is extremely simple: just use the name of the desired
variable, then the equal sign (=) and the value you want to put into the variable.
var = 1
print(var)
var = 1
account_balance = 1000.0
print(var)
var = 1
print(var)
var = var + 1
print(var)
Scenario
Here is a short story:
Once upon a time in Appleland, John had three apples, Mary had five apples, and Adam had six
apples. They were all very happy and lived for a long time. End of story.
Scenario
Miles and kilometers are units of length or distance.
Bearing in mind that 1 mile is equal to approximately 1.61 kilometers, complete the program so
that it converts:
miles to kilometers;
kilometers to miles.
kilometers = 12.25
miles = 7.38
Scenario
Take a look at the code : it reads a float value, puts it into a variable named x, and prints the
value of a variable named y. Your task is to complete the code in order to evaluate the following
expression:
3x3 - 2x2 + 3x - 1
The result should be assigned to y.
x = # Hardcode your test data here.
x = float(x)
# Write your code here.
print("y =", y)
String operators
fnam = input("May I have your first name, please? ")
lnam = input("May I have your last name, please? ")
print("Thank you.")
print("\nYour name is " + fnam + " " + lnam + ".")
Replication
The * (asterisk) sign, when applied to a string and number (or a number and string, as it remains
commutative in this position) becomes a replication operator:
string * number
number * string
print("+" + 10 * "-" + "+")
print(("|" + " " * 10 + "|\n") * 5, end="")
print("+" + 10 * "-" + "+")
Type conversions
str()
You already know how to use the int() and float() functions to convert a string into a
number.This type of conversion is not a one-way street. You can also convert a number into a
string, which is way easier and safer ‒ this kind of operation is always possible.
leg_a = float(input("Input first leg length: "))
leg_b = float(input("Input second leg length: "))
print("Hypotenuse length is " + str((leg_a**2 + leg_b**2) ** .5))