Lec3 Python Programming
Lec3 Python Programming
Programming
LEC 3
By: Youssef Abdel-mohymen
Table of contents
01 02
Data Handling Data Types
05
Concatenation
03 04
Variables Escape Sequences Characters
01
Data Handling
Dealing with Data in Python
● Python’s Role:
○ Code: The instructions you write to work with data.
○ Data: The information you handle, like numbers,
text, or true/false values.
● How They Work Together:
○ Code tells the computer what to do with the data.
○ Data is what you’re working with, and code helps
you process it.
Our Apps = Code + Data
The Building Blocks: Code & Data
Code Data
The set of instructions The information your
you write to interact with code processes, such as
data. numbers, text, or logical
values.
Key Insight: Python uses code to manipulate data, making apps functional and dynamic.
Structuring Data in Python
● Data Categories:
1. Numbers (Num): Used for any kind of quantitative data.
○ Examples: Student ages (21, 18), phone numbers
(1234567890), salaries ($5000).
2. Strings: Used to store text, including words or sentences.
○ Examples: Student names ("Alice"), teacher names
("Mr. Smith"), event names ("Graduation Day").
3. Booleans: Represent true or false values.
○ Examples: Has the teacher received their salary?
(True), Is the student's age appropriate? (False).
Working with Data Using Code
● Using Code to Handle Data:
1. Addition: Combine pieces of data.
○ Example: total_salary = base_salary + bonus
Adds a bonus to the base salary to get the total salary.
2. Modification: Change existing data.
○ Example: name = "Jane Doe"
Updates the name stored in the variable to "Jane Doe".
3. Deletion: Remove unwanted data.
○ Example: del student_age
Deletes the student_age variable, removing it from
memory.
● Key Insight:
○ Python code allows you to easily add, change, and delete
data, making your programs flexible and powerful.
Summary: Mastering Data in Python
● Core Concepts:
1. Python Combines Code and Data: Together,
they drive your programs.
2. Understanding Data Types: Knowing how to
use different data types is key to programming.
3. Importance of Variables: Variables help you
store and manage data in memory.
● Final Thought:
○ Successfully managing and manipulating data is
what makes Python such a powerful tool in
software development.
02
Data Types
Introduction to `type()` in Python
● Purpose: The type() function helps you find out what
type of data you're working with.
● Concept: In Python, everything is an object, and
type() reveals what kind of object each piece of data
is.
● Code Example:
print(type(10)) # Output: <class 'int'> (Integer)
print(type("Hello")) # Output: <class 'str'> (String)
print(type([1, 2, 3])) # Output: <class 'list'> (List)
Data Types: Integers and Floating Point Numbers
Source code
Translation
Compilation(or Interpretation)
Execution at Run-Time
Understanding Reserved Words in Python
● Reserved Words: Words that have special meaning in Python and
cannot be used as variable names.
○ Example: `if`, `else`, `while`, `for`, `def`, etc.
● Why They Matter: These words define the structure and flow of
your program.
○ Example: The `if` keyword is used to create conditional
statements.
● Python Command:
help("keywords")
● List of Common Reserved Words:
○ `if`, `else`, `elif`: Used for conditional statements.
○ `while`, `for`: Used for loops.
○ `def`: Used to define functions.
○ `class`: Used to define classes.
○ `return`: Used to return a value from a function.
Variable Assignment and Unpacking
● Multiple Assignment: Assigning multiple values to multiple
variables in a single line.
○ Example:
a, b, c = 1, 2, 3
print(a) # Outputs: 1
print(b) # Outputs: 2
print(c) # Outputs: 3
● Unpacking: Directly assigning values from a sequence (like
a tuple or list) to variables.
○ Example:
x, y = [4, 5]
print(x) # Outputs: 4
print(y) # Outputs: 5
Common Mistakes in Variable Assignment
● Incomplete Assignment:
a, b, c = 1, 2, # Error: not enough values to unpack
○ Explanation: This raises a Value Error because the third
variable c has no value assigned.
● Too Many Values:
a, b, c = 1, 2, 3, 4 # Error: too many values to unpack
○ Explanation: This raises a Value Error because there are
more values than variables.
● Examples of Correct Assignments:
● Example 1:
a, b, c = 1, 2, 3 # Correct, all variables have a value
● Example 2:
a, b, c = 1, 2 # Incorrect, raises Value Error
04
Escape Sequences
Characters
Escape Sequence Characters in Python
● What are Escape Sequence Characters?
○ Special characters that begin with a backslash \ used to perform
certain tasks within strings.
○ They allow you to insert special characters, manage formatting,
and more within your string data.
● List of Common Escape Sequences:
○ \b: Backspace
○ \newline: New Line
○ \\: Backslash
○ \': Single Quote
○ \": Double Quote
○ \n: Line Feed (New Line)
○ \r: Carriage Return
○ \t: Horizontal Tab
○ \xhh: Character Hex Value
Backspace and New Line Escape Sequences
● Backspace (\b): Removes the character before it.
○ Example:
print("Hello\bWorld") # Outputs: HellWorld
○ Explanation: The \b removes the 'o' from "Hello".
● New Line (\n): Inserts a new line.
○ Example:
print("Hello\nWorld") # Outputs: Hello
# World
○ Explanation: The \n moves the cursor to a new line
after "Hello".
Escaping Special Characters in Python
● Escape Backslash (\\): Prints a single backslash.
○ Example:
print("I Love Back Slash \\") # Outputs: I Love Back Slash \
● Escape Single Quote (\'): Allows using single quotes inside a single-
quoted string.
○ Example:
print('I Love Single Quote \'Test\'') # Outputs: I Love Single Quote 'Test'
● Escape Double Quote (\"): Allows using double quotes inside a double-
quoted string.
○ Example:
print("I Love Double Quotes \"Test\"") # Outputs: I Love
Double Quotes "Test"
Formatting with Line Feed, Carriage Return, and Tabs
● Line Feed (\n): Moves to the next line in the output.
○ Example:
print("Hello World\nSecond Line") # Outputs: Hello World #
Second Line
● Carriage Return (\r): Moves the cursor back to the beginning of
the line.
○ Example:
print("123456\rAbcde") # Outputs: Abcde6
○ Explanation: \r causes "Abcde" to overwrite "12345".
● Horizontal Tab (\t): Inserts a horizontal tab space.
○ Example:
print("Hello\tPython") # Outputs: Hello Python
Using Hexadecimal Values to Show Characters
● Hexadecimal Character Representation (\xhh):
○ A way to show characters using special codes called hexadecimal values.
○ What is ASCII?
■ ASCII stands for American Standard Code for Information
Interchange.
■ It’s a system that gives every character (like letters, numbers, and
symbols) a specific number. For example:
● The letter 'A' is the number 65 in ASCII.
● The letter 'a' is the number 97 in ASCII.
■ These numbers can also be shown as hexadecimal values, like \x41
for 'A' and \x61 for 'a'.
○ Example:
print("\x4F\x73") # Outputs: Os
● Explanation:
○ \x4F is the hexadecimal code for the letter 'O'.
○ \x73 is the hexadecimal code for the letter 's'.
○ When you put them together, \x4F\x73 shows the word "Os".
ASCII Table
05
Concatenation
Understanding String Concatenation in Python
● What is String Concatenation?
Concatenation is the process of joining two or more strings
together.
In Python, the + operator is used to concatenate strings.
○ Example:
● Example:
full = msg + " " + lang
print(full) # Outputs: I Love Python
● Explanation: The `msg` and `lang` variables
are concatenated and stored in the full variable,
which is then printed.
Multi-Line String Concatenation
● Using Backslash (\) for Multi-Line Strings:
○ You can use a backslash (\) to continue a string onto the next line
without breaking it.
○ Example:
a = "First \
Second \
Third"
b = "A \
B\
C"
print(a + "\n" + b)
○ Explanation: The strings a and b are created across multiple
lines and then concatenated with a newline (\n) in between.
Common Concatenation Errors
● Concatenating Strings with Non-Strings:
○ Python does not allow concatenating a string with a non-
string (e.g., an integer) directly.
● Example of an Error:
print("Hello " + 1) # Error: TypeError
● Explanation: Attempting to concatenate a string with an
integer without converting the integer to a string will result in a
TypeError.
● Fixing the Error:
● Convert the non-string to a string before concatenating:
print("Hello " + str(1)) # Outputs: Hello 1
Recap and Practice
● Key Takeaways:
○ Concatenation combines strings using the + operator.
○ Multi-line strings can be created using a backslash (\).
○ Ensure all concatenated elements are strings to avoid
errors.
● Practice Exercise:
○ Concatenate the following strings and print the result:
first_name = "John"
last_name = "Doe"
age = 30
print(first_name + " " + last_name + " is " + str(age) + " years old.")
Task 1
● Objective:
○ Create a new file to start writing the code in it, then at the top of the file write a multi-line
comment describing the file and you can write whatever you want, no problem..
● Instructions:
○ Create a File:
Open your editor and save a new file with a .py extension (e.g., task_1.py).