) (File Handlingالتعامل مع الملفات ⃣️1
open().توفر دوال لقراءة وكتابة الملفات عبر دالة Python
(Modes):األوضاع
للقراءة فقط o "r":
للكتابة (يمسح محتوى الملف السابق) o "w":
لإللحاق في نهاية الملف o "a":
:مثال قراءة ملف
with open("[Link]", "r") as file:
)(content = [Link]
)print(content
:مثال كتابة ملف
with open("[Link]", "w") as file:
)"\nهذا نص جديد"([Link]
:مثال اإللحاق
with open("[Link]", "a") as file:
)"\nسطر إضافي"([Link]
.تعني االنتقال لسطر جديد : \nمالحظة
للعمل مع البيانات Pandasمكتبة ⃣️2
NumPy.مكتبة قوية لتحليل البيانات وهي تعتمد على Pandas
Pandas:استيراد
import pandas as pd اسم مختصر للمكتبة # pd
جدول ثنائي األبعاد (صفوف +أعمدة) DataFrame:
{(df = [Link]
"Name": ["Ali", "Sara", "Khalid"],
]"Age": [25, 30, 22
)}
)print(df
:العمليات الشائعة
]": df["Ageالوصول لألعمدة o
)(: df["Age"].uniqueالقيم الفريدة o
Boolean:شرط o
df["Adult"] = df["Age"] >= 18
حفظ نسخة جديدة منDataFrame:
new_df = df[df["Age"] > 25]
3️⃣ مكتبةNumPy لألعداد والمصفوفات
NumPy مكتبة أساسية للعمليات العددية والمصفوفات.
إنشاء مصفوفة:
import numpy as np
arr = [Link]([1, 2, 3, 4])
خصائص المصفوفة:
print([Link]) # نوع البيانات
print([Link]) # عدد العناصر
print([Link]) # عدد األبعاد
الوصول والتقطيع:
print(arr[0]) # العنصر األول
print(arr[1:3]) # 2 إلى1 عناصر من الفهرس
4️⃣ ( العمليات على المصفوفاتVector & Matrix Operations)
جمع وطرح المصفوفات:
a = [Link]([1,2,3])
b = [Link]([4,5,6])
print(a + b) # [5 7 9]
print(a - b) # [-3 -3 -3]
( الضرب بالعددScalar Multiplication):
print(a * 2) # [2 4 6]
عنصر-ب-( ضرب عنصرHadamard Product):
print(a * b) # [4 10 18]
( الضرب القياسيDot Product):
print([Link](a, b)) # 32 = 1*4 + 2*5 + 3*6
) (2D Arraysالمصفوفات ثنائية األبعاد ⃣️5
D:إنشاء مصفوفة 2
matrix = [Link]([[1,2,3],
)]][4,5,6
صفوف وأعمدة )print([Link]) # (2,3
)print([Link] عدد العناصر الكلي # 6
:الوصول للعناصر
)]print(matrix[0,1 الصف ،0العمود # 2 → 1
للرسوم البيانية Matplotlibالربط مع مكتبة ⃣️6
:إلنشاء الرسوم البيانية بسهولة Matplotlibمع NumPyيستخدم
import [Link] as plt
)]x = [Link]([1,2,3,4
y = x**2
)[Link](x, y
)([Link]
الخالصة مع األمثلة العملية ✅
الموضوع الوصف مثال
File open("[Link]",
قراءة ،كتابة ،إلحاق الملفات
Handling )""r
Pandas DataFramesتحليل بيانات باستخدام )(df["Age"].unique
NumPy
مصفوفات أحادية أو ثنائية األبعاد )][Link]([1,2,3
Arrays
Vector ، dotجمع ،طرح ،ضرب ،ضرب عنصر-ب-عنصر
)a + b, [Link](a,b
Ops product
2D Arrays مصفوفات من صفوف وأعمدة ]matrix[0,1
Matplotlib رسم البيانات )[Link](x,y
Module 4 Summary: Working
with Data in Python
Congratulations! You have completed this module. At this point, you know that:
Python uses the open() function and allows you to read and write files, providing access
to the content within the file for reading. It also allows overwriting it for writing and
specifies the file mode (for example, r for reading, w for writing, a for appending).
To read a file, Python uses an open function along with r.
Python uses the open with function to read and process a file attribute, that is, from
open to close.
In Python, you use the open method to edit or overwrite a file.
To write a file, Python uses the open function along with w.
In Python, "a" indicates that the program has appended to the file.
In Python, “\n” signifies that the code should start on a new line.
Python uses various methods to print lines from attributes.
Pandas is a powerful Python library for data manipulation and analysis, providing data
structures and functions to work with structured data like data frames and series.
You import the file (panda) by using the import command followed by the file name.
In Python, you use the as command to provide a shorter name for the file.
In Pandas, you use a data frame (df) to specify the files to read.
DataFrames consist of rows and columns.
You can create new DataFrames by using the column or columns of a specific
DataFrame.
We can work with data in a DataFrames and save the results in different formats.
In Python, you use the Unique method to determine unique elements in a column of
the DataFrames.
You use the inequality operator along with df to assign a Boolean value to the selected
column in DataFrames.
You save a new DataFrame as a different DataFrame, which may contain values from
an earlier DataFrame.
NumPy is a Python library for numerical and matrix operations, offering multidimensional
array objects and a variety of mathematical functions to work with data efficiently.
NumPy is a basis for Pandas.
A NumPy array or ND array is similar to a list, usually of a fixed size with the same kind
of element.
A one-dimensional NumPy array is a linear sequence of elements with a single axis, like
a traditional list, but optimized for numerical computations and array operations.
You can access elements in a NumPy using an index.
You use the attribute dtype to get the data type of the array elements.
You use size and ndim to get the size and dimension of the array, respectively.
You can use indexing and slicing methods in NumPy.
Vector additions are widely used operations in Python.
Representing vector addition with line segments or arrows is useful.
NumPy codes work much faster, which is helpful with lots of data.
You perform vector subtraction by replacing the addition sign with a negative sign.
Multiplying an array by a scalar in Python entails multiplying each element of the array
by the scalar value, leading to a new array in which each element scales by the scalar.
Hadamard product refers to the element-wise multiplication of two arrays of the same
shape, resulting in a new array where each element is the product of the corresponding
elements in the input arrays.
The dot product in Python is the sum of the element-wise products of two arrays, often
used for vector and matrix operations to find the scalar result of multiplying
corresponding elements and summing them.
When working with NumPy, it is common to utilize libraries like Matplotlib to create
graphs and visualizations from numerical data stored in NumPy arrays.
A two-dimensional NumPy array is a grid-like structure with rows and columns suitable
for representing data as a matrix or a table for numerical computations.
In NumPy, "shape" refers to an array's dimensions (number of rows and columns),
indicating its size and structure.
You use the attribute "size" to obtain the size of an array.
You use rectangular attributes to access the various elements in an array.
You use a scalar to multiply elements in NumPy.
Glossary: Working with Data in
Python
Welcome! This alphabetized glossary contains many of the terms you'll find within this course. This
comprehensive glossary also includes additional industry-recognized terms not used in course
videos. These terms are important for you to recognize when working in the industry, participating in
user groups, and participating in other certificate programs.
Term Definition
A .csv (Comma-Separated Values) file is a plain text file format for storing tabular
.csv file data, where each line represents a row and uses commas to separate values in
different columns.
A .txt (Text) file is a common file format that contains plain text without specific
.txt file
formatting, making it suitable for storing and editing textual data.
To "append" means to add or attach something to the end of an existing object,
Append typically used in the context of adding data to a file or elements to a data structure like
a list in Python.
An "attribute" in Python refers to a property or characteristic associated with an
Attribute
object, which can be accessed using dot notation.
Broadcasting in NumPy allows arrays with different shapes to be combined in
Broadcasting in
element-wise operations by automatically extending smaller arrays to match the
NumPy
shape of larger ones, making operations more flexible.
In NumPy, a "component" typically refers to a specific element or value within a
Component
multi-dimensional array, which can be accessed using indexing.
Computation in NumPy involves performing numerical operations on arrays and
Computation matrices, making it a powerful library for mathematical and scientific computing in
Python.
Data analysis is the process of inspecting, cleaning, transforming, and interpreting
Data analysis
data to discover useful information, draw conclusions, and support decision-making.
A DataFrames in Pandas is a two-dimensional, tabular data structure for storing
DataFrames
and analyzing data, consisting of rows and columns.
Dependencies in Pandas are external libraries or modules, such as NumPy, that
Dependencies
Pandas rely on for fundamental data manipulation and analysis functionality.
File attributes generally refer to properties or metadata associated with files,
File attribute
which are managed at the operating system level.
File object A "file object" in Python represents an open file, allowing reading from or writing to
Term Definition
the file.
In Python, a "grid" typically refers to a two-dimensional structure composed of
Grid rows and columns, often used to represent data in a tabular format or for organizing
objects in a coordinate system.
The Hadamard product is a mathematical operation that involves element-wise
Hadamard multiplication of two matrices or arrays of the same shape, producing a new matrix
Product with each element being the product of the corresponding elements in the input
matrices.
To import Pandas in Python, you use the statement: import pandas as pd, which
Importing
allows you to access Pandas functions and data structures using the abbreviation
pandas
"pd."
In Python, an "index" typically refers to a position or identifier used to access
Index
elements within a sequence or data structure, such as a list or string.
Libraries in Python are collections of pre-written code modules that provide
Libraries
reusable functions and classes to simplify and enhance software development.
In Python, "linspace" refers to a NumPy function that generates an array of evenly
Linspace
spaced values within a specified range.
NumPy in Python is a fundamental library for numerical computing that provides
NumPy support for large, multi-dimensional arrays and matrices, as well as a variety of high-
level mathematical functions to operate on these arrays.
One
A one-dimensional NumPy array is a linear data structure that stores elements in
dimensional
a single sequence, often used for numerical computations and data manipulation.
NumPy
In Python, the "open" function is used to access and manipulate files, allowing you
Open function
to read from or write to a specified file.
Pandas is a popular Python library for data manipulation and analysis, offering
Pandas
data structures and tools for working with structured data like tables and time series.
Term Definition
Pandas library in Python refer to the various modules and functions within the
Pandas library Pandas library, which provides powerful data structures and data analysis tools for
working with structured data.
Plotting Plotting mathematical functions in Python involves using libraries like Matplotlib to
Mathematical create graphical representations of mathematical equations, aiding visualization, and
Functions analysis.
In NumPy, "shape" refers to an array's dimensions (number of rows and columns),
Shape
describing its size and structure.
Slicing in NumPy entails extracting specific portions of an array by specifying a
Slicing
range of indices, enabling you to work with subsets of the data.
Two A two-dimensional NumPy array is a structured data representation with rows and
dimensional columns, resembling a matrix or table, ideal for various data manipulation and
NumPy analysis tasks.
Universal functions (ufuncs) in NumPy are functions that operate element-wise on
Universal
arrays, providing efficient and vectorized operations for a wide range of mathematical
Functions
and logical operations.
Vector addition in Python involves adding corresponding elements of two or more
Vector addition
vectors, producing a new vector with the sum of their components.
Visualizations in Python refer to the creation of graphical representations, such as
Visualizations
charts, plots, and graphs, to illustrate and communicate data and trends visually.