You are on page 1of 14

3/24/2023

Assist. Lecturer: Hersh Mustafa Hama

Hersh.hama@uor.edu.krd

Spring Semester 2023

A Program to Print (Hello world!)


 A simple program that displays “Hello, World!”.
 It is often used to illustrate the syntax of the language.

 In this program, we have used the built-in print() function to print the string
Hello, world! on our screen.
 By the way, a string is a sequence of characters.
 In Python, strings are enclosed inside single quotes, double quotes, or triple quotes.

1
3/24/2023

Comment in Python
 Comments are very important while writing a program.
 They describe what is going on inside a program.
 You might forget the key details of the program you just wrote in a month's time.
 So taking the time to explain these concepts.
 In the form of comments is always fruitful.
 Single-line Comments: Hash (#) symbol is used to write a single line comment in
python, which is put at the beginning of each line.
 Multi-line Comments: triple quotes, either ''' or """ used to write multi-line
comments, these triple quotes are put at the beginning and end of the comment.
3

Comment in Python: Example

2
3/24/2023

Python Keywords
 Keywords are the reserved words in Python.
 There are many keywords in Python
keywords
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
5

Python Identifiers
 An identifier is a name given to entities like class, functions, variables, etc.
 It helps to differentiate one entity from another.
 Rules for writing identifiers:
 Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A
to Z) or digits (0 to 9) or an underscore (_). Example: (Var_1)
 An identifier cannot start with a digit. Example: (1var)
 Keywords cannot be used as identifiers.
 We cannot use special symbols like !, @, #, $, % etc. in our identifier.
 An identifier can be of any length.
 We cannot use space or arithmetic operators such as +, *, -, /, ^ etc. 6

3
3/24/2023

Python Statement
 Instructions that a Python interpreter can execute are called statements.
 For example, var = 1 is an assignment statement.
 if statement, for statement, while statement, etc. are other kinds of
statements which will be discussed later.
 Multi-line statement, the end of a statement is marked by a newline character. But
we can make a statement extend over multiple lines with the line continuation
character (\). OR, we can implement inside parentheses.

Python Variables

 A variable is a named location used to store data in the memory.


 It is helpful to think of variables as a container that holds data that can be changed
later in the program.
 n = 10
 Here, we have created a variable named n.
 We have assigned the value 10 to the variable.
 num = 10
 num = 15.7
 Initially, the value of num was 10. Later, it was changed to 15.7
8

4
3/24/2023

Assigning Values to Variables


 You can use the assignment operator = to assign a value to a variable.
 Example #1: Declaring and assigning value to a variable

 In this program, we assigned a value raparinuni.org to the variable webName.


 Then, we printed out the value assigned to webName (i.e. raparinuni.org)
 Example #2: Changing the value of a variable

 Here, we have assigned raparinuni.org to the website variable initially.


 Then, the value is changed to uor.edu.krd. 9

Assigning Values to Variables


 Example #3: Assigning multiple values to multiple variables.

 If you want to assign the same value to multiple variables at once, you can do it as:

 The this program assigns the “Python” string to all the three variables CL, Lan
and CPL.
 Multiple statements on a Single Line: The semicolon (;) allows multiple
statements on the single line.
10

5
3/24/2023

Data Types
 Python has some standard types that are used to define the operations possible on
them and the storage method for each of them.
 The data stored in memory can be of many types. For example:
 Student’s name is stored as string
 Student’s age is stored as numeric (int)
 Student’s degree is stored as floating point (float)
 The Boolean type has only two possible values: True, or False. (bool)
 Also, complex numbers fall under Python numbers category. (complex)
 We can use the type() function to know which class a variable.
 There are five standard data types such as (Numbers, String, List, Tuple, Dictionary)
11

Data Types: Example

OUTPUT

12

6
3/24/2023

Numbers
 Number data types store numeric values.
 Number objects are created when you assign a value to them.
 You can delete a single object or multiple objects by using the del statement.

13

String in Python
 Strings in Python are identified as a contiguous set of characters represented in the
quotation marks.
 The plus(+) sign is the string concatenation operator
 The asterisk(*) is the repetition operator.

Output

14

7
3/24/2023

Sequence Types
 In Python programming, sequences are a generic term for an ordered set which
means that the order in which we input the items will be the same when we access
them.
 Python supports six different types of sequences.
 These are strings, lists, tuples, byte sequences, byte arrays, and range objects.
 Here, we will discuss types of them:
 Lists: they are mutable, heterogeneous
 Tuples: they are immutable, heterogeneous.
 Range objects: they are immutable, homogeneous.
15

List
 Lists are Python’s most flexible ordered collection object type.
 All the items in a list do not need to be of the same type.
 Declaring a list is pretty straightforward.
 Items separated by commas are enclosed within brackets [ ].
 We can use the slicing operator [] to extract an item or a range of items from a list.
 The index starts from 0 in Python.
 The plus (+) sign is the list concatenation operator.
 The asterisk (*) is the repetition operator.
 Lists are mutable, meaning, the value of elements of a list can be altered.
16

8
3/24/2023

List: Example

OUTPUT

17

List: Methods
 Python has a set of built-in methods that you can use on lists:
 append(): Adds an element at the end of the list.
 remove(): Removes the first item with the specified value.
 pop(): Removes the element at the specified position.
 clear(): Removes all the elements from the list.
 copy(): Returns a copy of the list.
 insert(): Adds an element at the specified position.
 extend(): Add the elements of a list, to the end of the current list.
 index(): Returns the index of the first element with the specified value
 reverse(): Reverses the order of the list.
 sort(): Sort the list. 18

9
3/24/2023

List Methods: Examples #1

OUTPUT

19

List Methods: Examples #2

OUTPUT

20

10
3/24/2023

Python List Functions


 Commonly Used Python List Functions
 sum(): Adds all elements of a list.
 len(): Returns the number of elements in the list.
 min(): Gets the smallest element in a sequence.
 max(): Gets the smallest element in a sequence.

21

Tuple
 Tuple is an ordered sequence of items same as a list.
 Tuple is immutable, it means once created cannot be modified.
 It is defined within parentheses () where items are separated by commas.
 Tuples can be thought of as read-only lists.

OUTPUT

22

11
3/24/2023

Range() Function
 The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and stops before a specified number.
 Syntax: range(start, stop, step)
 Start: An integer number specifying at which position to start. (Default is 0)
 Stop: An integer number specifying at which position to stop (not included).
 Step: it is optional. An integer number specifying the increment. (Default is 1)
Note: Start and step are optional, but stop is required.

23

Python Set
 Set is an unordered collection of unique (no duplicates) items.
 Set is defined by values separated by comma inside braces { }.
 Items in a set are not ordered.
 Elements must be immutable (cannot be changed).
 You cannot access items in a set by referring to an index or a key.
 Methods on sets:
 Setname.add(one element)
 Setname.update([add multiple elements])
 Setname.remove(remove an element)
 Setname.clear(): it is used to remove all elements in the set. 24

12
3/24/2023

Set: Example

OUTPUT

25

Python Dictionary
 Dictionary is an unordered collection of key-value pairs.
 It is generally used when we have a huge amount of data. Dictionaries are optimized
for retrieving data. We must know the key to retrieve the value.
 In Python, dictionaries are defined within braces {} with each item being a pair in
the form key:value. Key and value can be of any type.
 Syntax: DictionaryName = {key:value, key:value, key:value}

26

13
3/24/2023

Data Type Conversion


 We can convert between different data types by using different type conversion
functions like int(), float(), str(), etc.
 Conversion from float to int will truncate the value (make it closer to zero).

OUTPUT

27

14

You might also like