0% found this document useful (0 votes)
7 views88 pages

Computer All Python

The document provides an introduction to Python, covering its features, history, and applications in data analysis. It also explains Google Colab as a cloud platform for running Python, along with basic Python concepts such as print functions, data types, type casting, strings, input operations, and conditional statements. Each section includes examples and exercises to reinforce learning.

Uploaded by

Tahseen Mahdi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views88 pages

Computer All Python

The document provides an introduction to Python, covering its features, history, and applications in data analysis. It also explains Google Colab as a cloud platform for running Python, along with basic Python concepts such as print functions, data types, type casting, strings, input operations, and conditional statements. Each section includes examples and exercises to reinforce learning.

Uploaded by

Tahseen Mahdi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Python

By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
What is Python?

Python is a powerful and easy-to-learn programming language


used for building applications, analyzing data, and developing
artificial intelligence systems.

•High-level: Python manages complex computer memory, letting you focus on the
data problem.
•Interpreted: Code executes line by line, making it fast to test and debug.
•General-purpose: Versatile but excels in data science due to its rich ecosystem.
Python History

Python was created in the late 1980s by Guido van Rossum at the
Centrum Wiskunde & Informatica (CWI) in the Netherlands and was
first released in 1991. Van Rossum’s goal was to create a language
that was easy to read and simple to use, yet powerful enough to
bridge the gap between low-level languages like C and shell
scripting.
The name Python

Guido van Rossum was a big fan of the British comedy group Monty Python and their TV show,
Monty Python's Flying Circus. He was looking for a short, unique, and slightly mysterious name
for his invention, and he decided to name it after the show.
Why Python for Data Analysis?

- Vast Ecosystem of Libraries: It offers specialized tools (libraries) for data


manipulation, cleaning, and analysis, like Pandas and NumPy.

- Readability: Its clean syntax makes data analysis code easy to read, share, and
maintain.
- Versatility: You can use Python for the entire data pipeline: extraction, cleaning,
analysis, and modeling.
Thanks

• Questions?
Google Colab
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
What is Google Colab?

• Free cloud platform by Google


• Run Python in your browser
• No installation required
• Widely used for Data Science and AI
How to Access Google Colab

1. Go to [Link]
2. Sign in with your Google account
3. Click 'New Notebook'
Colab Interface

• Notebook name at the top


• Code cells
• Text cells
• Run button to execute code
Working with Code Cells

• Write Python code inside a cell


• Click the Run button or press Shift + Enter
• The output appears below the cell
Example Python Code

• print('Hello from Google Colab')


Saving and Sharing

• Notebooks are saved automatically in Google Drive


• You can share them like Google Docs
• Export as .ipynb or .pdf
Advantages of Colab

• Free GPU support


• Easy collaboration
• Works on any device
• Ideal for teaching and learning Python
Thanks

• Questions?
Print () & Comments
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
Print()

In Python, the print() function is used to display output on the screen


(console). It is one of the most basic and commonly used functions in Python.
Printing a simple text

print("Hello World")
Printing a variable

name = "Ali"
print(name)
Printing numbers

print(10)
print(5 + 3)
Printing multiple values

name = "Ali"
age = 25
print(name, age)
Printing text with a variable

name = "Ali"
print("My name is", name)
Comments

In Python, comments are used to explain the code and make it easier to
understand. Comments are ignored by the Python interpreter, so they do not
affect program execution.
Single-line comment

# This is a comment
print("Hello World")
Comment after a line of code

age = 20 # This variable stores the age


print(age)
Multi-line comments

# This program
# prints a welcome message
# for the user
print("Welcome")
Docstring comments (triple quotes)

"""
This program prints
a welcome message
"""
print("Hello")
Example with Comments
# This program prints student information
# Store the student's name
name = "Ali"
# Store the student's age
output
age = 20
Student Name: Ali
# Store the student's university
Age: 20
university = "University of Baghdad"
University: University of Baghdad
# Print the information
print("Student Name:", name)
print("Age:", age)
print("University:", university)
Thanks

• Questions?
Data Types
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
Numeric

x = 10 # int
y = 3.5 # float
z = 2 + 3j # complex

print(x)
print(y)
print(z)
string

Used to store text.


Strings are written inside quotation marks.
name = "Ali"
city = 'Baghdad'

print(name)
print(city)
Boolean

x = True
y = False

print(x)
print(y)
List

A collection of items that can be changed (mutable).


Lists are written inside square brackets [ ].
numbers = [10, 20, 30, 40]

print(numbers)
Tuple

Similar to a list, but cannot be changed (immutable).


Tuples are written inside parentheses ( ).
numbers = (10, 20, 30)

print(numbers)
Set

A collection of unique items (no duplicates).


Sets are written inside curly brackets { }.
numbers = {1, 2, 3, 4}

print(numbers)
Dictionary

Stores data as key–value pairs.


student = {
"name": "Ali",
"age": 20,
"city": "Baghdad"
}

print(student)
Data Type Example
int 10
float 3.5
string "Ali"
boolean True
list ]1,2,3[
tuple )1,2,3(
set }1,2,3{
dictionary {"name":"Ali"}
Example shows how different data types can be used in one program

# Different data types


name = "Ali" # string
age = 25 # int
height = 1.75 # float Output
is_student = True # boolean Ali
courses = ["Python", "Data Analytics", "AI"] # list 25
student_info = {"name": "Ali", "age": 25} # dictionary 1.75
True
print(name)
['Python', 'Data Analytics',
print(age) 'AI']
print(height) {'name': 'Ali', 'age': 25}
print(is_student)
print(courses)
print(student_info)
Thanks

• Questions?
Type Casting
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
Type Casting

Type Casting means converting a variable from one data type to another.
• In Python, this is done using built-in functions such as:
• int() → convert to integer
• float() → convert to float
• str() → convert to string
• bool() → convert to boolean
Converting to Integer

x = int(5.8)
print(x)
Converting to Float

x = float(10)
print(x)
Converting to String

x = str(100)
print(x)
print(type(x))
Converting Input to Integer

age = int(input("Enter your age: "))


print(age + 5)
Function Converts To
int() Integer
float() Float
str() String
bool() Boolean
Example Including All Type Casting Cases in Python
# Type Casting Example
# string to integer
num_str = "10"
num_int = int(num_str)
# integer to float
num_float = float(num_int)
# integer to string
num_text = str(num_int)
# integer to boolean
num_bool = bool(num_int)

print("Original string:", num_str)


print("Converted to int:", num_int)
print("Converted to float:", num_float)
print("Converted to string:", num_text)
print("Converted to boolean:", num_bool)
Thanks

• Questions?
Strings
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
Creating a String

A string in Python is a sequence of characters used to store text such as


names, messages, or sentences.
Creating a String

name = "Ali"
print(name)
Using Single or Double Quotes

city = 'Baghdad'
country = "Iraq"
print(city)
print(country)
String with Numbers and Symbols

message = "Welcome to Python 2026!"


print(message)
Multiple Words String

sentence = "Python is easy to learn"


print(sentence)
Example for Students

student_name = "Sara"
major = "Computer Science"
print("Student:", student_name)
print("Major:", major)
Accessing Characters (Indexing)

• In Python, you can access any character in a string using indexing.


Each character in a string has a position number (index).
• The index always starts from 0.
Accessing Characters (Indexing)

• text = "Python"
• print(text[0]) # P
• print(text[1]) # y
• print(text[2]) # t
• print(text[3]) # h
• print(text[4]) # o
• print(text[5]) # n
Accessing Characters (Indexing)

Character P y t h o n
Index 0 1 2 3 4 5
Accessing Characters from the End (Negative
Indexing)

text = "Python"
print(text[-1]) # n
print(text[-2]) # o
print(text[-3]) # h
Accessing Characters from the End (Negative
Indexing)

text = "Python"
print(text[-1]) # n
print(text[-2]) # o
print(text[-3]) # h
String Concatenation

Character P y t h o n
Negative
6- 5- 4- 3- 2- 1-
Index
Print the First and Last Character

city = "Baghdad"
print(city[0]) # First character
print(city[-1]) # Last character
Exercises

• Create a variable called name and store your name in it.


Print the first character of the name.
• Create a variable called city = "Baghdad".
Print:
The first character
The last character
Exercises

• word = "Computer" Create a variable called city = "Baghdad".


Print the third character of the word.
• country = "Iraq“
Print each character using indexing.
• text = "University“
Write a program that prints the last two characters of the string above.
Exercises
• language = "Python" country = "Iraq“
Print:
The second character
The fourth character
The last character

• Store your college name in a variable. text = "University“

• Print:
• First letter
• Middle letter
• Last letter
Thanks

• Questions?
Input & Mathematical
Operations
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
Input

Input allows the program to receive data from the user during execution.
Ex1:
name = input("Enter your name: ")
print("Hello", name)
Ex2:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
Mathematical Operations

Operation Symbol Example


Addition + a+b
Subtraction - a-b
Multiplication * a*b
Division / a/b
Modulus % a%b
Exponent ** a ** b
Floor Division // a // b
Addition +

Addition is used to add two numbers.


a=5
b=3
result = a + b
print(result)
Subtraction -

Subtraction is used to subtract one number from another.


a = 10
b=4
result = a - b
print(result)
Multiplication *

Multiplication is used to multiply two numbers.


a=6
b=5
result = a * b
print(result)
Division /

Division is used to divide one number by another


a = 10
b=2
result = a / b
print(result)
Modulus %

Modulus returns the remainder of a division.


A = 10
b=3
result = a % b
print(result)
Exponent **

Exponent is used to raise a number to a power.


a=2
b=3
result = a ** b
print(result)
Floor Division //

Floor division returns the integer part of the division (without decimals).
a = 10
b=3
result = a // b
print(result)
EX3: without input
a = 10
b=3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
EX4: with input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("Addition =", num1 + num2)


print("Subtraction =", num1 - num2)
print("Multiplication =", num1 * num2)
print("Division =", num1 / num2)
print("Modulus =", num1 % num2)
print("Exponent =", num1 ** num2)
Thanks

• Questions?
If, if :else, if:elif:else
in Python
By
Dr. Hiba Mohammad Fadil Dr. Ali Yahya Gheni
if

The if statement executes code only if the condition is true.


if condition:
statement
EX:
age = 20

if age >= 18:


print("You are an adult")
If: else
Used when the program must choose between two alternatives.
if condition:
statement1
else:
statement2
EX:
num = 7

if num % 2 == 0:
print("Even number")
else:
print("Odd number")
If: elif: else
Used when there are multiple conditions.
if condition1:
statement
elif condition2:
statement
else:
statement
EX:
score = 85
if score >= 90:
print("Grade A")
elif score >= 70:
print("Grade B")
else:
print("Grade C")
Example with User Input

score = int(input("Enter your score: "))


if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("Try again")
Thanks

• Questions?

You might also like