Prelim - Python Introdhdhdjejejej
Prelim - Python Introdhdhdjejejej
It is used for:
Data Science
Machine Learning
Web Development
Computer Vision and Image processing
Embedded Systems and IoT
Job Scheduling and Automation
Desktop GUI Applications
Console-based Applications
CAD Applications
Game Development
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it
is written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Python Syntax compared to other programming
languages
Python was designed for readability, and has some similarities to the English language
with influence from mathematics.
Python uses new lines to complete a command, as opposed to other programming
languages which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the scope of
loops, functions and classes. Other programming languages often use curly-brackets for
this purpose.
Hello, World!
Python Install
Many PCs and Macs will have python already installed.
To check if you have python installed on a Windows PC, search in the start bar for Python or
run the following on the Command Line (cmd.exe):
If you find that you do not have Python installed on your computer, then you can download
it for free from the following website: https://www.python.org/
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write
Python (.py) files in a text editor and then put those files into the python interpreter to be
executed.
The way to run a python file is like this on the command line:
Let's write our first Python file, called helloworld.py, which can be done in any text editor.
Hello, World!
Simple as that. Save your file. Open your command line, navigate to the directory where you
saved your file, and run:
C:\Users\Your Name>python
Or, if the "python" command did not work, you can try "py":
C:\Users\Your Name>py
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
In [7]: if 5 > 2:
print("Five is greater than two!")
In [3]: if 5 > 2:
print("Five is greater than two!")
else:
print('asdgasdg')
The number of spaces is up to you as a programmer, the most common use is four, but it
has to be at least one.
In [ ]: if 5 > 2:
print("Five is greater than two!")
print("")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise Python will
give you an error:
In [1]: if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
In Python, variables are created when you assign a value to it:
In [1]: x = 0
y = "Hello, World!"
print(x)
print(y)
0
Hello, World!
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Hello, World!
Multiline Comments
Python does not really have a syntax for multiline comments.
Since Python will ignore string literals that are not assigned to a variable, you can add a
multiline string (triple quotes) in your code, and place your comment inside it:
#def subtraction(x,y):
# print(f"The difference of {x} and {y} is: {x-y}")
def multiplication(x,y):
print(f"The product of {x} and {y} is: {x*y}")
def division(x,y):
print(f"The quotient of {x} and {y} is: {x/y}")
while True:
print("+-----------------------------+")
print("A-ddition")
print("S-ubtraction")
print("M-ultiplication")
print("D-ivision")
print("E-xit")
print("+-----------------------------+")
choice = input("Enter your choice: ")
choice = choice.upper()
if choice == 'E':
break
else:
"""val1 = int(input("Enter 1st number: "))
val2 = int(input("Enter 1st number: "))
if choice =='A':
addition(val1,val2)
elif choice =='S':
subtraction(val1,val2)
elif choice =='M':
multiplication(val1,val2)
elif choice =='D':
division(val1,val2)
else:
print("Invalid Input!")"""
print("Thank You! Good Bye!")
A-ddition
S-ubtraction
M-ultiplication
D-ivision
E-xit
The sum of 12 and 12 is: 24
A-ddition
S-ubtraction
M-ultiplication
D-ivision
E-xit
Thank You! Good Bye!
As long as the string is not assigned to a variable, Python will read the code, but then ignore
it, and you have made a multiline comment.
Creating Variables
Python has no command for declaring a variable.
In [3]: x = 5
y = "John"
print(x)
print(y)
5
John
Variables do not need to be declared with any particular type, and can even change type
after they have been set.
4a
Casting
If you want to specify the data type of a variable, this can be done with casting.
In [4]: x = 5
y = "John"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>
In [5]: x = "John"
# is the same as
x = 'John'
Case-Sensitive
Variable names are case-sensitive.
In [6]: a = 4
A = "Sally"
print(A)
print(a)
#A will not overwrite a
Sally
4
Camel Case
Pascal Case
Snake Case
print(myVariableName,MyVariableName,my_variable_name)
Orange
Banana
Cherry
Orange
Orange
Orange
Unpack a Collection
In [12]: fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
apple
banana
cherry
Output Variables
The Python print() function is often used to output variables.
In [13]: x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Python is awesome
Python is awesome
Global Variables
Variables that are created outside of a function (as in all of the examples in the previous
pages) are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
In [15]: x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Python is awesome
If you create a variable with the same name inside a function, this variable will be local, and
can only be used inside the function. The global variable with the same name will remain as
it was, global and with the original value.
In [16]: x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Python is fantastic
Python is awesome
myfunc()
print("Python is " + x)
Python is fantastic
Also, use the global keyword if you want to change a global variable inside a function.
In [18]: x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python is fantastic
In [19]: val = bytearray("Hello", 'utf-8')
print(val)
b1 = bytes([65, 66, 67, 68, 69])
print(b1)
bytearray(b'Hello')
b'ABCDE'