Lecture 2
Lecture 2
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.
Compilation vs. interpretation
Your Program
COMPILATION INTERPRETATION
Compilation vs. •the execution of the translated code is usually
interpretation faster; •you can run the code as soon as you complete it - there are no
•only the user has to have the compiler - the additional phases of translation;
end-user may use the code without it; •the code is stored using programming language, not the
ADVANTAGES •the translated code is stored using machine machine one - this means that it can be run on computers using
language - as it is very hard to understand it, your different machine languages; you don't compile your code
own inventions and programming tricks are likely separately for each different architecture.
to remain your secret.
•the compilation itself may be a very time-
•don't expect that interpretation will ramp your code to high
consuming process - you may not be able to run
speed - your code will share the computer's power with the
your code immediately after any amendment;
DISADVANTAGES •you have to have as many compilers as
interpreter, so it can't be really fast;
•both you and the end user have to have the interpreter to run
hardware platforms you want your code to be
your code.
ML run on.
Note: languages designed to be utilized in the interpretation manner are often called scripting languages,
while the source programs encoded using them are called scripts.
What is Python?
Python was created by Guido van Rossum, born in 1956 in Haarlem, the Netherlands.
In December 1989, I was looking for a "hobby" programming project that would keep me occupied during the week around Christmas. My office (...) would be
closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about
lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big
fan of Monty Python's Flying Circus).
Guido van Rossum
Python goals
•an easy and intuitive language just as powerful as those of the major competitors;
•open source, so anyone can contribute to its development;
•code that is as understandable as plain English;
•suitable for everyday tasks, allowing for short development times.
What makes Python special? Python has two direct competitors Where can we see Python in action?
Internet services like search engines, cloud storage and tools,
Perl - a scripting language originally authored social media and so on.
by Larry Wall; Many developing tools are implemented in Python. More and
more everyday use applications are being written in Python.
Ruby - a scripting language originally authored Lots of scientists have abandoned expensive proprietary tools
by Yukihiro Matsumoto. and switched to Python. Lots of IT project testers have started
using Python to carry out repeatable test procedures.
Python’s versions
Guido van Rossum used the "C" programming language to implement the very first version of
his language and this decision is still in force. All Pythons coming from the PSF are written in
the "C" language. This is why the PSF implementation is often referred to as CPython.
https://www.python.org/psf-landing/
✓ the fundamentals of computer programming, i.e., how the computer works,
how the program is executed, how the programming language is defined and
constructed;
✓ the basic information about Python and how it is positioned among other
programming languages, and what distinguishes its different versions;
✓ the study resources and different types of interfaces you will be using in the
course.
Literals - the data in itself
A literal is data whose values are determined by the literal itself.
Example: 123 is a literal, and c is not.
print("2") string
print(2)
Literal – Integer
Integers
English Python
11,111,111 11.111.111 11 111 111 11111111 11_111_111 +or - 11_111_111
When you want to use any numbers that are very large or very small, you can use scientific notation
8
300000000 = 3 x 10
Strings
Strings are used when you need to process text (like names of all kinds, addresses, novels, etc.), not numbers.
a name;
a value (the content of the container)
If you want to give a name to a variable, you must follow some strict rules:
1. the name of the variable must be composed of upper-case or lower-case letters, digits, and
the character _ (underscore)
2. the name of the variable must begin with a letter;
3. the underscore character is a letter;
4. upper- and lower-case letters are treated as different (a little differently than in the real
world - Alice and ALICE are the same first names, but in Python they are two different
variable names, and consequently, two different variables);
5. the name of the variable must not be any of Python's reserved words (the keywords - we'll
explain more about this soon).
Correct and incorrect variable names
Here are some correct, but not always convenient variable names:
incorrect names:
10t (does not begin with a letter), Exchange Rate (contains a space)
Keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Creating variables
var = 1
print(var) Note: False Not equals false
Using variables
var = 1
account_balance = 1000.0
client_name = 'John Doe'
print(var, account_balance, client_name) Shortcut operators
print(var)
x = x * 2 ⇒ x*=2
Sheep = Sheep + 1 ⇒ Sheep+=1
i = i + 2 * j ⇒ i += 2 * j
var = "3.8.5"
print("Python version: " + var)
var = var / 2 ⇒ var /= 2
var = 1 x = x ** 2 ⇒ x **= 2
var = 100
print(var) # OT = 1
var = 200 + 300
var = var + 1
print(var )# OT = 500
print(var) # OT = 2
Leaving comments in code: why, how, and when
You may want to put in a few words addressed not to Python but to humans, usually to explain to other readers of the code
how the tricks used in the code work, or the meanings of the variables, and eventually, in order to keep stored information on
who the author is and when the program was written.
A remark inserted into the program, which is omitted at runtime, is called a comment
print("Tell me anything...")
anything = input()
print("Hmm...", anything, "... Really?")
Python offers two simple functions to specify a type of data and solve this problem - here they are: int() and float().
fnam = input("May I have your first name, please? ") print("+" + 10 * "-" + "+")
lnam = input("May I have your last name, please? ") print(("|" + " " * 10 + "|\n") * 5, end="")
print("Thank you.") print("+" + 10 * "-" + "+")
print("\nYour name is " + fnam + " " + lnam + ".")
See you in the lab session