0% found this document useful (0 votes)
4 views

1 - Introduction To Python

Uploaded by

Hafsa Zahran
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1 - Introduction To Python

Uploaded by

Hafsa Zahran
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Python

Fundamentals

Friday, August 9, 2024 1


Contents

• Introduction To Programming
• What is Python?
• Why Python?
• Data Types & Variables
• Operations
• Casting
• Take input from user
• Lists
• Tuples
• Dictionaries
• Sets
2
Contents

• Conditions
• Loops
• Functions
• Functions with parametars
• Important Libiraries
• Numpy
• Matplotlib
• Pandas
• Seaborn
• Scipy

3
Agenda

• Introduction To Programming
• What is Python?
• Why Python?
• Data Types & Variables
• Operations
• Casting
• Take input from user

4
Introduction To Programming

• Programming is the art of giving instructions to a computer to perform specific tasks. In the context
of data analysis, programming allows us to manipulate, analyze, and visualize data effectively.
• Python, a versatile and beginner-friendly programming language, has gained immense popularity in
the field of data analysis due to its simplicity, readability, and powerful libraries.

5
01110000 01110010 01101001 01101110 01110100 00101000 00110010 00101011
00110010 00101001

6
asm
.section .data
result:
.quad 4

.section .text
.globl _start
_start:
mov $1, %rax
mov $1, %rdi
lea result(%rip), %rsi
mov $8, %rdx
syscall

mov $60, %rax


xor %rdi, %rdi
syscall

7
#include <iostream>

int main() {
std::cout << 2 + 2 << std::endl;
return 0;
}

8
print(2+2)

9
Compiler vs Interpreter

10
What is Python?

• Python is an interpreted, object-oriented, high-level programming language with dynamic


semantics.

• Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it
very attractive for Rapid Application Development, as well as for use as a scripting or glue language
to connect existing components together.

11
Why Python?

• Why Python? Python has emerged as the go-to language for data analysis and data science due to
several factors:

• Versatility: Python can handle a wide range of tasks, from simple scripting to complex data
manipulation and machine learning.

• Rich Ecosystem: Python boasts a vast collection of libraries specialized for data analysis, such
as Pandas, NumPy, and Matplotlib, making data manipulation and visualization convenient.

• Community Support: Python has a large and active community of developers, which means
extensive documentation, tutorials, and community-driven resources are readily available.

• Python's flexibility and accessibility make it an ideal choice for beginners venturing into the world of
data analysis.

12
Variable Declaration and Syntax
Rules

13
Variables

14
15
16
Variables
• Definition of Variables
• In programming, variables are containers for storing data values. These values can be numbers, strings, lists, or
other data types.
• Variables allow us to label and manipulate data within a program, making it easier to work with and
understand.
• Declaring Variables in Python
• In Python, variables are created by assigning a value to a name using the "=" (assignment) operator.
• Python is dynamically typed, meaning you don't need to specify the data type of a variable explicitly. Python
infers the data type based on the assigned value.
• Variable Naming Conventions
• Variable names in Python can contain letters, numbers, and underscores, but they cannot start with a number.
• It's good practice to use descriptive and meaningful names for variables to enhance code readability.
• Variable names should be written in lowercase letters, with words separated by underscores (snake_case).

17
Variables
Examples
Example 1: Example 2:
age = 25 temperature_celsius = 20.5
name = "John" is_raining = True
Print(name) Print(temperature_celsius)
print(age) print(is_raining)

Output: Output:
John 20.5
25 True

18
19
20
Data Types

21
Data Types

Python – Data
Types

Sequence
Numeric Boolean Dictionary Set
Type

Complex
Integer Float String List Tuple
Number

22
Data Types: Numeric

•Integers – This value is represented by int class. It contains positive or negative whole numbers
(without fractions or decimals). In Python, there is no limit to how long an integer value can be.

•Float – This value is represented by the float class. It is a real number with a floating-point
representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive
or negative integer may be appended to specify scientific notation.

•Complex Numbers – A complex number is represented by a complex class. It is specified as (real


part) + (imaginary part)j. For example – 2+3j

23
Data Types: Boolean

Boolean
Boolean objects that are equal to True are truthy (true), and those equal to False are falsie (false).

Note:
None, False, 0 , Empty collections: “”, (), [], { }

Output:
print(type(True))
print(type(False)) <class 'bool'>
<class 'bool'>

24
Operators

25
Operators

26
Operators

27
Boolean Operators
Expression (Logic Gate) Expression

28
29
30
Let's Practice

Arithmetic Operators | HackerRank

31
Data Types: Sequence

String
Strings in Python are arrays of bytes representing Unicode characters. A string is a collection of one or
more characters put in a single quote, double-quote, or triple-quote. In Python, there is no character data
type Python, a character is a string of length one. It is represented by str class.

string = ‘Hello world!’


print(string)

32
Using your knowledge of escape sequences, create a
single print() statement with single string inside of it
that displays this when the program is run:

33
Data Types: List

List
Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very
flexible as the items in a list do not need to be of the same type.

List = [“Ahmed", “Mohamed", “Sara"] Output:

print("\nList containing multiple values: ")


Ahmed
print(List[0]) Sara
print(List[2])

34
Data Types: Tuple

Tuple
Just like a list, a tuple is also an ordered collection of Python objects. The only difference between a
tuple and a list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is
represented by a tuple class.

tuple1 = ([1, 2, 3, 4, 5])


print("First element of tuple") Output:
print(tuple1[0])
First element of tuple
print("\nLast element of tuple")
1
print(tuple1[-1]) Last element of tuple
print("\nThird last element of tuple") 5
Third last element of tuple
print(tuple1[-3]) 3
35
Data Types: Set

Set
A Set is an unordered collection of data types that is iteratable, mutable, and has no duplicate elements.
The order of elements in a set is undefined though it may consist of various elements.

Output:
set =([1, 2, ‘Ahmed', 4, ‘Sara', 6])
print("\nSet with the use of Mixed Values")
Set with the use of Mixed Values
print(set) {1, 2, 4, 6, ‘Ahmed’, Sara'}

36
Data Types: Dictionary

Dictionary
A dictionary in Python is an unordered collection of data values, used to store data values like a map,
unlike other Python Data Types that hold only a single value as an element, a Dictionary holds a key:
value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a
Dictionary is separated by a colon : , whereas each key is separated by a ‘comma’.

student = { "name": "John Doe", "age": 20, "courses": ["Math", "Science"], Output:
"grade": "A" }
name = student["name"]
John Doe
print(name) Not Available
address = student.get("address", "Not Available")
print(address)

37
Let’s Practice

38
Practice
• Question 1:
• Write a Program To Calculate an are of rectangle (area = L x W)
Ans:

39
Practice
• Question 2:
• Write a program that reads the radius of a circle and calculates the area and circumference then prints the
results.
Ans:

40
Casting
• What is Casting?
• Casting refers to the process of converting a variable from one data type to another. In Python, casting allows us
to change the data type of a variable as needed during program execution.
• Types of Casting in Python
• There are two types of casting in Python:
• Implicit Casting: Automatic conversion of data types by Python based on context.
• Explicit Casting: Manual conversion of data types using built-in functions like int() , float() , str(),etc.

41
Taking Input From User
• In Python, you can prompt the user to enter data during program execution using the input() function.
• The input() function reads a line of text entered by the user from the keyboard and returns it as a string.

Note that the input is always of type str, which is


important if you want the user to enter numbers.
Therefore, you need to convert the str before trying to
use it as a number

42
Operations
• Summary Question:
Write a program that take two integers from the user and print the results of this equation: Result = ((num1 + num2) * 3) – 10
Ans:

43
Any Questions ?

44
Thank You ♥♥

45

You might also like