COMP 2 - Computer Application For ME - WK1-4
COMP 2 - Computer Application For ME - WK1-4
Python Programming
What is Python?
• Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.
• It is used for:
• python –version
• print("Hello, World!")
Hello, World!
Congratulations, you have written and
executed your first Python program.
Python Indentation
• Indentation refers to the spaces at the beginning
of a code line.
• if 5 > 2:
• print("Five is greater than two!")
• if 5 > 2:
• print("Five is greater than two!")
Output
• Five is greater than two!
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:
• Example • Output
• if 5 > 2: • File
• print("Five is greater than two!") "demo_indentation2_error.py",
• print("Five is greater than line 3
two!")
• print("Five is greater
than two!")
• ^
• IndentationError: unexpected
indent
Python Variables
• In Python, variables are created when you assign a
value to it:
• Example
• Variables in Python:
•x = 5
• y = "Hello, World!"
• Example: • Output:
•x = 5
• y = "Hello, World!"
• print(x) 5
Hello, World!
• print(y)
Comments
• Python has commenting capability for the purpose of
in-code documentation.
• Example
• Comments in Python: #This is a comment.
print("Hello, World!")
• Example: • Output:
• #This is a comment. • Hello, World!
• print("Hello, World!")
Python Comments
• Comments can be used to explain Python code.
Example: Output:
• Example:
• Output:
• Hello, World!
Creating a Comment
• A comment does not have
to be text that explains
the code, it can also be
used to prevent Python
from executing code:
Output:
Example:
• Cheers, Mate!
• #print("Hello, World!")
• print("Cheers, Mate!")
Multiline Comments
• Python does not really have a
syntax for multiline comments.
• To add a multiline comment you
could insert a # for each
line:
Example: Output:
• #This is a comment
• Hello, World!
• #written in
• #more than just one line
• print("Hello, World!")
Multiline Comments
• Or, not quite as intended, you can use
a multiline string.
As long as the string is
not assigned to a variable,
• Since Python will ignore string Python will read the code,
literals that are not assigned to a but then ignore it, and you
variable, you can add a multiline
string (triple quotes) in your code,
have made a multiline
and place your comment inside it: comment.
Example:
• """ Output:
• This is a comment
• written in
• more than just one line
• Hello, World!
• """
• print("Hello, World!")
Python Variables
Variables
• Variables are containers for storing data values.
Creating Variables
• Python has no command for declaring a variable.
• A variable is created the moment you first assign
a value to it.
Example: Output:
•x = 5 •5
• y = "John" John
• print(x)
• print(y)
Variables do not need to be declared with any
particular type, and can even change type after
they have been set.
Example: Output:
•x = 4 # x is of type • Sally
int
• x = "Sally" # x is now of
type str
• print(x)
Casting
• If you want to specify the data type of a
variable, this can be done with casting.
Examples:
Output:
•3
•3
• 3.0
Get the Type
Example:
• x = 5
• y = "John"
• print(type(x))
• print(type(y))
Get the Type
Output:
• <class 'int'>
<class 'str'>
Single or Double Quotes?
• x = "John"
• # is the same as
• x = 'John'
Single or Double Quotes?
• John
• John
Case-Sensitive
Example:
This will create two variables:
•a = 4
• A = "Sally"
• #A will not overwrite a
Case-Sensitive
Output:
This will create two variables:
•4
• Sally
Python - Variable Names
Variable Names
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume). Rules for
Python variables:
• File "./prog.py",
• 2myvar = "John" line 1 2myvar =
• my-var = "John" "John" ^
• my var = "John" SyntaxError:
invalid syntax
During handling of
• #This example will the above
produce an error in exception, another
the result exception occurred:
Multi Words Variable Names
myVariableName = "John"
Pascal Case
MyVariableName = "John"
Snake Case
my_variable_name = "John"
Python Variables - Assign
Multiple Values
Many Values to Multiple Variables
Example:
• print(x)
• print(y)
• print(z)
Note: Make sure the number of variables matches the number of values, or else
you will get an error.
Python Variables - Assign
Multiple Values
Output:
• Orange
• Banana
• Cherry
One Value to Multiple Variables
Example:
• x = y = z = "Orange"
• print(x)
• print(y)
• print(z)
One Value to Multiple Variables
Output:
• Orange
• Orange
• Orange
Unpack a Collection
Example:
Unpack a list:
Output:
Unpack a list:
• apple
• banana
• cherry
Unpack a Collection
Output:
Unpack a list:
• apple
• banana
• cherry
Python - Output Variables
Output Variables
Example:
• x = "Python is awesome"
• print(x)
Python - Output Variables
Output Variables
Output:
• Python is awesome
Python - Output Variables
Output Variables
Example:
• x = "Python"
• y = "is"
• z = "awesome"
• print(x, y, z)
Python - Output Variables
Output Variables
Example:
• x = "Python "
• y = "is "
• z = "awesome"
• print(x + y + z)
Notice the space character after "Python " and "is ", without them the
result would be "Pythonisawesome".
Python - Output Variables
Output Variables
Output:
• Python is awesome
Python - Output Variables
Output Variables
Example:
•x = 5
• y = 10
• print(x + y)
Python - Output Variables
Output Variables
Ouput:
• 15
Python - Output Variables
Output Variables
Example:
• x = 5
• y = "John"
• print(x + y)
Python - Output Variables
Output Variables
Output:
• TypeError: unsupported operand type(s) for +: 'int' and
'str'
Python - Output Variables
Output Variables
Example:
• x = 5
• y = "John"
• print(x, y)
Python - Output Variables
Output Variables
Output:
• 5 John
Python - Global Variables
Global Variables
• x = "awesome"
• def myfunc():
• print("Python is " + x)
• myfunc()
Output:
Create a variable outside of a function, and use
it inside the function
• Python is awesome
Python - Global Variables
Global Variables
• x = "awesome"
• def myfunc():
• x = "fantastic"
• print("Python is " + x)
• myfunc()
• print("Python is " + x)
Output:
Create a variable inside a function, with the same
name as the global variable
• Python is fantastic
• Python is awesome