python_vs
python_vs
Python
• Interpreter:
- Translate one statement at the time.
- Slower execution time.
- Memory efficient
- Generate error on first met.
- Examples:
Python, Ruby, PHP
• Compiler:
- Scans the entire program and translates it as a whole into machine code.
- Faster execution time.
- Generate error after whole translation
- Examples:
C family, GO lang
What is programming languages
• High-level languages:
- Closer to the human language.
- Easier to write, shorter, but slower runtime.
- Examples:
C, C++, C#, PASCAL, R, Python, Java, JS, VB.
• Low-level languages:
- closer to the native language of the computer(binary).
- Harder to write, longer but faster runtime, most of the time used for specific
hardware (i.e. pilot’s helmet).
- Example:
Assembly, Machine Code.
Python
• Web development
• Data Science
• Machine Learning and Artificial Intelligence
• IoT programming
• System automation
• Game Development
• Web Scraping Applications
• Desktop GUI
• etc
Basic terms & practice
Basic terms:
• Variables: String, Integer, float, Boolean.
• Arithmetic operators: +, -, *, **, /, %, //
• Comparison operators: <, >, ==, !=, <= , >=, and , or , not
Examples:
1: msg = “hello world”
print(msg)
2: msg = “hello”
msg = msg + “world”
print(msg)
3: x = 10
y = 2
print( x + y )
print( x - y )
print( x / y )
print( x * y )
print( x ** y )
print( x // y )
print( x % y )
Basic terms & practice
4: x = “H”
y = 3110
z = “ world”
print( x + y + z)
What is the problem? How to fix it? Answer: casting to string.
print( x + str(y) + z )
More terms:
• Loop - “while” loops and “for” loops.
• Data structure - List
• Function.
6: lst = []
print (len(lst))
lst = range(10)
print(lst)
print(lst[1])
print(lst[-1])
print(lst[11])
End of first lesson – while, for and what are lists
Python documentation
• https://docs.python.org/3.8/
• Use google
• https://stackoverflow.com/
Don’t be afraid.
You can learn anything, if you want!!!
”While” loop
With the ”while” loop we can execute a set of statements as long as a condition is true.
i=1
while i < 6:
print(i)
i += 1
With the ”break” statement we can stop the loop even if the while condition is true:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
”While” loop, cont
With the ”continue” statement we can stop the current iteration, and continue with the
next:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
”While” loop exercise
We will build a fortune teller program that will guess numbers
1. Define a secret number in a variable named “secret”, choose any number you want
2. Receive an input from user. This will be his guess number
3. While the guess number is not equal to the “secret” number, the program will ask the user
for a new input
4. If the user guessed the number correctly, the program will print
“That’s right, the number is <secret>”
Data type: list
List is a collection collection of arbitrary objects. Allows duplicate members.
List functions
• Add an element to a list: a.append(’bob’)
• Print(a) -> ['foo', 'bar', 'baz', 'qux’, ‘bob’]
• Check how many elements are in the list: len(a)
• Print(len(a)) -> 5
• Deleting an element from list
• Del a[0] -> print(a) -> ['bar', 'baz', 'qux’, ‘bob’]
• Removing an element from a list
• a.remove(’qux’) -> print(a) -> ['bar', 'baz', ‘bob’]
• Changing a value of a list element
• a[2] = ‘new value’ -> print(a) -> ['foo', 'bar', ‘new value', 'qux’, ‘bob’]
Data type: list, cont
• Accessing list elements
• print(a[0]) -> ‘foo’
• print(a[-1]) -> ‘corge’
• a[0] == [-6] ?
• List slicing
• print(a[0:2]) -> [’foo’, ‘bar’] == a[-6:-4]
• List concatenation
• a = [10, 20] + a -> print(a) -> [10, 20, 'foo', 'bar', ‘new value', 'qux’, ‘bob’]
”for” loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
• Define two lists: “passwords”, “usernames”. Populate those lists with arbitrary values
• Define a third list that will hold all the combinations of usernames and passwords
• Iterate over the “usernames” list, iterate over the “passwords” list
• Append the combination of ”username” and “password” separated by colon (“:”)
• Print all combinations and the count of all combinations
Python functions
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
def my_function():
print("Hello from a function")
def is_logged_in(username):
if username == ‘root’:
print(“root is logged in”)
else:
print(username + “ is not logged in”)
Python functions, cont
def is_password_correct(password):
if password == ‘passw0rd’:
return True
else:
return False
Exercise:
Which popular module in python is used to make HTTP requests?
Python modules - Exercise
Build a program to make an http request to get your public IP (use website:
http://ifconfig.co/ip)