4. DCIT22 Getting Started With Python 1
4. DCIT22 Getting Started With Python 1
Lesson 3:
Getting Started
with Python
Prepared by:
Ms.Mariella R. Leyba, MIT
PYTHON
Indentation
● C, C++, and Java use curly braces {} to
define a block of code.
● Python uses indentation.
Example:
JAVA PYTHON
Comments
● In Python, we use the hash (#) symbol
to start writing a comment. MULTI-LINE COMMENTS
- For multiple strings.
Example:
#This is a comment """This is also a
#print out Hello perfect example of
print('Hello') multi-line comments"""
Output:
Hello
PYTHON IDENTIFIER
Identifier num = 1
identifier literal
● is the name given to entities like
class, functions, variables etc. in
Python.
Keywords
● are the reserved words in Python.
1. Numbers
• Integers
>>> num = 1234
>>> print(num)
1234
• Floating Point
- A floating point number is accurate up to decimal places.
2. Strings
- is a sequence of Unicode characters.
Single Quote
>>> sample = ‘This is a string.’
>>> print(sample)
‘This is a string.’
Double Quote
>>> sample = “This is a string.”
>>> print(sample)
‘This is a string.’
PYTHON DATA TYPES
3. List
- is an order sequence of items
Index:
sample[0] = 1
sample[1] = 2.2
sample[2] = ‘python’
4. Set
- is an unordered collection of unique items.
- they eliminate duplicates.
5. Tuple
- is an ordered sequence of items same as list, difference is that it cannot be modified.
Do not do this:
Conversion between Data Types
● List to Set
● Set to Tuple
● String to List
PYTHON OPERATORS
Operators
● are special symbols in Python that carry out arithmetic or logical computation.
● The value that the operator operates on is called the operand.
Here, + is the operator that performs addition. 2 and 3 are the operands and
5 is the output of the operation.
PYTHON OPERATORS
1. Arithmetic Operators
- are used to perform mathematical operations like addition, subtraction, multiplication etc.
PYTHON OPERATORS
2. Comparison Operators
- are used to compare values. It either returns True or False according to the condition.
PYTHON OPERATORS
3. Logical Operators
PYTHON OPERATORS
4. Assignment Operators
- Are used in Python to assign values to variables.
PYTHON OUTPUT
We use the print() function to output data to the standard output device.
PYTHON OUTPUT
● sep
- separator is used between the values. It defaults into a space.
print(1,2,3,4)
# Output: 1 2 3 4
print(1,2,3,4,sep='*')
# Output: 1*2*3*4
• Output Formatting
str.format() - this method is visible to any string object.
PYTHON INPUT
To allow flexibility we might want to take the input from the user.
In Python, we have the input() function to allow this.
Prompt - is the string we wish to display on the screen.
eval() - operation can be performed using this function. It can evaluate even expressions,
provided the input is a string.
Thank you and God bless!
___