Ceng240 Week 3
Ceng240 Week 3
Lecture-3
Lecture : Week-3
Subject : Introduction to Python
- Numbers and Boolean values,
- Container data (str, tuple, list, dict, set);
- Mutable - immutable data;
- Aliasing problem
Variables:
• In programming languages, we can
give a name to data and use that
name to access
Printing data:
• Python provides the print() function to
display data items on screen.
Expression Result
true and false false
true or false true
not false true
not true false
31
Fall 2023 Hüseyin Sayın - Ceng240 – Lecture-3 – Introduction to Python
Decision Making:
Equality and Relational Operators
Solution to Precedence Problems
When in doubt…
Use parentheses!!!
• Dictionary:
- Is not sequential.
- Element indexes are arbitrary.
• mutable:
- After created-just can wholly
destroyed.
- But cannot change or delete their
individual elements.
- Strings and Tuples are immutable.
- Lists, Dictionaries and Sets are
mutable.
• immutable:
- Adding new elements and changing
or deleting existing ones is possible.
- Lists, Dictionaries and Sets are
mutable.
-.
-
1. Number of elements
2. Concatenation
3. Repetition
4. Membership
1. Number of elements
- len() is a built-in function
- The count of elements in the
container.
2. Concatenation
- String, Tuple and List data types
can be combined using ‘+’ operation.
3. Repetition
- String, Tuple and List data types
can be combined using ‘*’ operation.
4. Membership
- All containers can be checked for
whether they contain a certain item as
an element using ‘in’ and ‘not in’
operation.
- The result is either True or False.
- For dictionaries in tests if the domain set
contains the element,
- For others it simply tests if element is a
member.
Fall 2023 Hüseyin Sayın - Ceng240 – Lecture-3 – Introduction to Python 61
Strings
• Hold a sequence of characters.
• It is a container where each element is a
character.
• Python does not have a special
representation for a single character.
Characters are represented externally as
strings containing a single character only
• Creation
- In addition to using quotes for string
creation, the str() function can be used to
create a string from its argument.
• Lists
• Membership:
Similar to strings, in and not in
operations can be used to check
whether a tuple/list contains an
element.
• Membership:
• len() and
• membership (in and not in) operations that we have
seen above.
• <dictionary>.values() and
• <dictionary>.keys() to obtain lists of values and keys
respectively.
• Frozenset:
- An immutable version of the set type.
- Can be constructed using the
frozenset() function.