Decision Making Tools Lecture # 02
Decision Making Tools Lecture # 02
Lecture # 02
1
Contents
● Basics of Python
● White Spaces and Indentation
● Comments
● Python docstring
● Identifiers
● String literals
● Variables
● Python strings
2
Basics of Python
3
Whitespaces and indentation
4
Advantages:
By using whitespace and indentation to organize the code, Python code gains the
following advantages:
● First, you’ll never miss the beginning or ending code of a block like in other
programming languages such as Java or C#.
● Second, the coding style is essentially uniform. If you have to maintain
another developer’s code, that code looks the same as yours.
● Third, the code is more readable and clear in comparison with other
programming languages.
5
Comments
● The comments are as important as the code because they describe why a
6
Python docstrings
One-line docstrings:
● A one-line docstring fits one line. A one-line docstring begins with triple quotes (""")
and also ends with triple quotes (""").
○ def quicksort():
Multi-line docstrings:
8
Identifiers
● Identifiers are names that identify variables, functions, modules, classes, and
other objects in Python.
● The name of an identifier needs to begin with a letter or underscore (_).
The following characters can be alphanumeric or underscore.
● Python identifiers are case-sensitive. For example, the counter and Counter
are different identifiers.
● You can not use python keywords for naming identifiers
9
Keywords
● Some words have special meanings in Python. They are called keywords.
● The following shows the list of keywords in Python:
○ False class finally is return
○ None continue for lambda try
○ True def from nonlocal while
○ and del global not with
○ as elif if or yield
○ assert else import pass
○ break except in raise
● Python is a growing and evolving language. So its keywords will keep increasing and
changing.
● Python provides a special module for listing its keywords called keyword.
○ import keyword
○ print(keyword.kwlist)
10
String literals
● Python uses single quotes ('), double quotes ("), triple single quotes (''') and
triple-double quotes (""") to denote a string literal.
● The string literal need to be surrounded with the same type of quotes.
● For example, if you use a single quote to start a string literal, you need to
use the same single quote to end it.
○ s = 'This is a string'
○ print(s)
○ s = "Another string using double quotes"
○ print(s)
○ s = ''' string can span
○ multiple line '''
○ print(s)
11
Variables
A variable can hold various values at different times. And its value can change
throughout the program.
12
Creating variables
● The = is the assignment operator. In this syntax, you assign a value to the variable_name.
● The value can be anything like a number, a string, etc., that you assign to the variable.
● The value can be anything like a number, string, etc., that you assign to the variable.
● The following defines a variable named counter and assigns the number 1 to it:
○ counter = 1
13
Naming variables
● Naming variable(s) requires knowledge about some rules. If you don’t follow them,
you’ll get error(s).
● The following are the variable rules that you should keep in mind:
○ Variable names can contain only letters, numbers, and underscores (_). They
can start with a letter or an underscore (_), not with a number.
○ Variable names cannot contain spaces. To separate words in variables, you
use underscores for example sorted_list.
○ Variable names cannot be the same as keywords, reserved words, and built-
in functions in Python.
● Variable names should be concise and descriptive. For example, the active_user
variable is more descriptive than the au.
● Use underscores (_) to separate multiple words in the variable names.
14
A simple Example
Output
print(myNumber) 3
4.5
myNumber2 = 4.5 Hello World!
print(myNumber2)
15
Introduction to Python string
● Up to this point, you probably know what is string and how to declare a
string.
○ A string is a series of characters. In Python, anything inside quotes is a
string. And you can use either single or double quotes. For example:
● Python will replace the {name} by the value of the name variable.
● When you place the string literals next to each other, Python
automatically concatenates them into one string. For example:
○ greeting = 'Good' 'Morning!'
○ print(greeting) Output: Good Morning!
18
Accessing string elements
● Since a string is a sequence of characters, you can access its elements
using an index. The first character in the string has an index of zero. For
example:
○ str = "Python String"
○ print(str[0]) # P
○ print(str[1]) # y
● If you use a negative index, Python returns the character starting from the
end of the string. For example:
○ str = "Python String"
○ print(str[-1]) # g
○ print(str[-2]) # n
● To get the length of a string, you use the len() function. For example:
○ str = "Python String"
○ str_len = len(str)
Output: 13
○ print(str_len)
● Slicing strings
20
Python strings are immutable
For example:
○ str = "Python String"
○ new_str = 'J' + str[1:] 21
○ print(new_str)
Summary
22