0% found this document useful (0 votes)
7 views

python_vs

Uploaded by

xmp2g745b2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

python_vs

Uploaded by

xmp2g745b2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Presentation topics:

What is programming languages

Python

Basic terms & practice


What is programming languages

• A special language programmers use to develop software programs, scripts, or other


sets of instructions for computers to execute.
• List of programming language:
https://en.wikipedia.org/wiki/List_of_programming_languages
• Each programming language has different concept, saved keywords and advantages
(and disadvantages).
• Hence, each programming language will best suite to different kind of task.
• Programming language could be cross-platform(running using VM) or platform
specific.
• Some of the programming language translated the code for machine language using
interpreter and some using compiler.
What is programming languages

• 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

• High level program


• Easy to write a code.
• Open-source
• Working with interpreter
• Cross platform
• Pycharm Community
• We will use python 3, python 2 will be no longer support on 2020.
What can you build with 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)

* Ask the student to practice that also.


How to run Python code

• Python source code is saved with “.py” extention


• Running a script: Python3 example.py
• Run Python code is through an interactive session
• Python3 [enter]
Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more
information.
>>>
Basic terms & practice

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 )

5: x = input(“enter first number\n”)


y = input(“enter second number\n”)
if x > y:
print( str(x) + “ is larger than “ + str(y) )
elif x < y:
print( str(y) + “ is larger than “ + str(x) )
else:
print( “equal numbers”)
Basic terms & practice
Student practice:
• Write a program that get two numbers as input from user.
• Then the program will shows the following menu asking the user for operation:
“ Please choose operation to do:
1. For addition numbers to each other please press ‘1’
2. For subtraction please press ‘2’
3. For multiplication numbers please press ‘3’
4. For Division numbers please press ‘4’ “
• The program will print to the screen the output of the operation that the user choose.
• If operation is not defined print “math error”
Comments in Python
• # This is a comment
• ”””
• This is a multi-line comment
• You can write anything you want
• ”””
Basic terms & practice

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.

a = ['foo', 'bar', 'baz', 'qux’]


print(a)
['foo', 'bar', 'baz', 'qux’]

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).

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
Exercise
We will build a program that will print all the combinations of user name and passwords

• 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")

my_function() -> "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

We have seen functions before:


• ”input” -> accepts a string and return string
• “print” -> accepts a string and returns None
Python modules
A module is a file containing Python definitions and statements. A module can define
functions, classes and variables. A module can also include runnable code. Grouping related
code into a module makes the code easier to understand and use.

Using a module in your code


• Import the module using an “import statement”
• import os
• Print(os.getlogin())

Installing modules (pip)

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)

• Import the requests module


• Make an http request to the website and save it to “r” variable
• Print the IP

You might also like