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

learn-python-in10-days-extract

The document is a guide titled 'Learn Python In 10 Days' by Younes Derfoufi, aimed at beginners who want to learn Python programming efficiently. It covers essential topics from installation and basic programming concepts to advanced topics like object-oriented programming and file manipulation, structured over ten days. The author, a seasoned educator in mathematics and computer science, provides practical examples to reinforce learning throughout the course.

Uploaded by

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

learn-python-in10-days-extract

The document is a guide titled 'Learn Python In 10 Days' by Younes Derfoufi, aimed at beginners who want to learn Python programming efficiently. It covers essential topics from installation and basic programming concepts to advanced topics like object-oriented programming and file manipulation, structured over ten days. The author, a seasoned educator in mathematics and computer science, provides practical examples to reinforce learning throughout the course.

Uploaded by

walidbahaj2021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Learn Python In 10 Days

Younes Derfoufi. Aggregated Doctor


Teacher of Computer Science and Mathematics
Research Professor
at CRMEF Oujda

11 avril 2024
Contents

1 Day 1: Installing the tools 11


1.1 Python Installation: An All in One Solution . . . . . . . 11
1.2 First Python Program . . . . . . . . . . . . . . . . . . . 12

2 Day 2 : Variables and Operators 19


2.1 Variables in Python . . . . . . . . . . . . . . . . . . . . 19
2.2 Comments in Python . . . . . . . . . . . . . . . . . . . . 21
2.3 Operators in Python . . . . . . . . . . . . . . . . . . . . 23
2.4 Python Functions . . . . . . . . . . . . . . . . . . . . . . 25
2.5 Reading keyboard input with the input() function . . . 26

3 Day 3 : Control Structures 28


3.1 The Selective Structure if ... else ... . . . . . . . . . . . 28
3.2 The elif Statement . . . . . . . . . . . . . . . . . . . . . 29
3.3 The repetitive structure for ... . . . . . . . . . . . . . . . 30
3.4 The repetitive structure while . . . . . . . . . . . . . . . 30
3.5 Python Exceptions (Try Except) . . . . . . . . . . . . . 31

4 Day 4 : Python Strings 34


4.1 Defining a String in Python . . . . . . . . . . . . . . . . 34
4.2 String Slicing in Python . . . . . . . . . . . . . . . . . . 36
4.3 Length of a String . . . . . . . . . . . . . . . . . . . . . 38
4.4 Accessing Elements of a String . . . . . . . . . . . . . . 38
4.5 String Operations . . . . . . . . . . . . . . . . . . . . . . 40
4.6 Methods associated with a Python string . . . . . . . . 41

1
CONTENTS 2

5 Day 5 : Lists in Python 46


5.1 Creating a List in Python . . . . . . . . . . . . . . . . . 46
5.2 Accessing Elements of a List . . . . . . . . . . . . . . . . 46
5.3 5.3 Change the value of a list element . . . . . . . . . . 48
5.4 List Slicing in Python . . . . . . . . . . . . . . . . . . . 48
5.5 Iterate over a list elements . . . . . . . . . . . . . . . . . 50
5.6 Length of a Python list . . . . . . . . . . . . . . . . . . 51
5.7 Add or remove items from the list . . . . . . . . . . . . 51
5.8 The different methods for Python lists . . . . . . . . . . 56

6 Day 6 : Tuples In Python 57


6.1 Defining a Python Tuple . . . . . . . . . . . . . . . . . . 57
6.2 Access the elements of a tuple . . . . . . . . . . . . . . . 58
6.3 Loop through a tuple . . . . . . . . . . . . . . . . . . . . 59
6.4 Check if an element exists in a tuple . . . . . . . . . . . 60
6.5 Length of a tuple . . . . . . . . . . . . . . . . . . . . . . 60
6.6 Deleting a tuple . . . . . . . . . . . . . . . . . . . . . . . 60
6.7 Methods associated with a tuple . . . . . . . . . . . . . 61

7 Day 7 : Dictionaries and sets in Python 62


7.1 Dictionaries in Python . . . . . . . . . . . . . . . . . . . 62
7.2 Python Sets . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.3 Fonction Lumbda En Python . . . . . . . . . . . . . . . 75
7.4 List comprehension in Python . . . . . . . . . . . . . . . 77

8 Day 8 : Object-Oriented Programming 78


8.1 The concept of OOP in Python . . . . . . . . . . . . . . 78
8.2 Terminologie de la POO . . . . . . . . . . . . . . . . . . 79
8.3 Classes in Python . . . . . . . . . . . . . . . . . . . . . . 80
8.4 Instance methods in OOP Python . . . . . . . . . . . . 82
8.5 Class methods in Python . . . . . . . . . . . . . . . . . 83
8.6 Instance attributes and class attributes . . . . . . . . . . 83
8.7 Static methods . . . . . . . . . . . . . . . . . . . . . . . 84
8.8 Inheritance in Python . . . . . . . . . . . . . . . . . . . 85
CONTENTS 3

9 Day 9 : Modules in Python 88


9.1 About a Python module . . . . . . . . . . . . . . . . . . 88
9.2 Create your own module . . . . . . . . . . . . . . . . . . 89
9.3 Standard Modules in Python . . . . . . . . . . . . . . . 90
9.4 Example of standard modules . . . . . . . . . . . . . . . 91

10 Day 10 : Files in Python 98


10.1 Python file (I/O) . . . . . . . . . . . . . . . . . . . . . . 98
10.2 File Opening Mode . . . . . . . . . . . . . . . . . . . . . 98
10.3 Opening and reading a file . . . . . . . . . . . . . . . . . 99
10.4 Opening files in write mode in Python . . . . . . . . . . 104
10.5 10.5 Summary of Python methods associated with a file
object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
About the Author

Younes Derfoufi is a research professor at CRMEF OUJDA, an


aggregated doctor with over 30 years of experience teaching mathe-
matics and computer science to teacher students at CRMF Oujda.
He has taught: about ten computer science modules at CRMEF Ou-
jda: front-end and back-end web development including HTML, CSS,
Bootstrap, Javascript, JQuery, PHP, MySql..., CMS-oriented web de-
velopment: Joomla, Wordpress, Moodle, Simple Machine, PHPBB...
Not forgetting web frameworks: Laravel, Django... He has also taught
object-oriented programming in Java, Dart & Flutter... He has also
taught Python programming: basics in Python, object-oriented pro-
gramming, SQLite databases... Python GUIs with Tkinter & PyQt5...
For around ten years, he taught information and communication tech-
nology: Multimedia ICT: Autoplay Media Studio, Video editing with
AVS Video Editor & Camtasia Studio, image processing with Gimp...
Mathematical ICT: Geogebra, algorithms with Algobox, LaTeX lan-
guage, Scientific Workplace, Statistics with SPSS, Statistics with Python
Numpy & Pandas... Before teaching all of these, the author worked
as a mathematics teacher for trainee teachers for about twenty years,
teaching in particular the following concepts: general algebra: groups,
rings, fields, polynomials with one or more variables, linear algebra:
vector spaces, matrices, bilinear form, quadratic forms, Euclidean and
Hermitian spaces, alternating multilinear forms & determinants... nu-
merical series, sequences & series of functions, power series analytic
functions..., Riemann integration, generalized integral, parameter de-
pendent, multiple integrals..., affine geometry, Euclidean vector geom-
etry, Euclidean affine geometry, probability & statistics... Despite all
this, the author had no difficulty teaching mathematics and computer

4
CONTENTS 5

science didactics, objective-based pedagogy, competency methodology,


mathematics and computer science learning planning, didactic pro-
duction, mathematical and computer science ICT. The author has
also participated in numerous national and international conferences
& seminars.
Copyright

ľ YOUNES DERFOUFI. All rights reserved. No part of this work


may be reproduced, distributed, or transmitted in any form or by any
means, without the prior written permission of the author, except in
cases permitted by copyright law. This work is protected by interna-
tional copyright laws and the author’s moral rights. Any violation of
copyright, including unauthorized reproduction, distribution, or mod-
ification of this work, may result in legal action and claims for dam-
ages. The author retains all exclusive rights to this work, including the
rights of reproduction, distribution, public performance, and modifica-
tion. Permission from the author must be obtained before any use or
exploitation of this work, whether commercial or non-commercial. For
permission requests or more information on the use of this work, please
contact the author at the following address: [author’s contact address].
The author reserves the right to make changes to this copyright text
at any time without notice. Please check this section regularly to en-
sure you are informed of the latest updates. Thank you for respecting
the copyright and exercising diligence in the use of this work. Your
cooperation is greatly appreciated.

6
Code Preview

The code employed in this book has been thoughtfully crafted uti-
lizing valuable tools and specific LaTeX packages. Here is an example
of code preview:
 
1 # define a string variable

2 s = " H e l l o World ! "

3 print ( s )
 

7
Who is This Book For?

The book "Python in 10 Days" is a practical guide designed


specifically for beginners passionate about quickly and effectively learn-
ing Python. This book is aimed at a diverse audience, ranging from
students eager for new skills to teachers looking to integrate program-
ming into their courses, to students wanting to strengthen their com-
puter knowledge.
Over ten days, we will guide you through the fundamentals
of Python, from initial installations to advanced concepts such as
object-oriented programming and file manipulation. Whether
you are an absolute beginner or already have experience in pro-
gramming, this book will walk you step by step through your journey
to becoming a competent Python developer. Each day, you will
explore a key aspect of the language, reinforce your knowledge with
practical examples, and be ready to move to the next level by the
end of each chapter. Get ready to dive into the fascinating world
of Python, and by the end of these ten days, you will be equipped
to create applications, solve complex problems, and harness the
full potential of this versatile programming language.

Day 1: Installing the Tools :


Welcome to your first day of exploring Python! Before diving into
code, let’s start with the essentials: installing the necessary tools for
Python development. This first step is crucial to ensure that your
development environment is properly configured. Get ready to dis-
cover the necessary steps to install Python and associated tools on

8
CONTENTS 9

your system.

Day 2: Variables and Operators


Now that your environment is set up, let’s get started! Today, you
will dive into the world of variables and operators in Python. Under-
stand how to store data in variables, manipulate this data with oper-
ators, and start building the foundations of your Python programs.

[...]
Continue exploring the various aspects of Python each day, learning
gradually and applying your newly acquired knowledge. Have a great
journey into the world of Python!
Introduction

Python is a high-level programming language known for its simplic-


ity and readability. Created by Guido van Rossum and first released
in 1991, Python has gained immense popularity due to its versatility
and ease of use. It is widely used in various fields such as web devel-
opment, data science, artificial intelligence, scientific computing, and
more.
One of the key features of Python is its clear and concise syntax,
which emphasizes readability and reduces the cost of program main-
tenance. This makes it an excellent choice for both beginners and
experienced programmers alike.
Python supports multiple programming paradigms, including pro-
cedural, object-oriented, and functional programming. It comes with a
comprehensive standard library that provides support for a wide range
of tasks, from file handling and networking to mathematical operations
and web development.
Another notable aspect of Python is its strong community sup-
port and active development ecosystem. There are numerous third-
party libraries and frameworks available, such as NumPy, Pandas,
Django, Flask, TensorFlow, and PyTorch, which extend the capabili-
ties of Python for various applications.
Overall, Python’s simplicity, readability, and vast ecosystem make
it a powerful and versatile language suitable for a wide range of pro-
gramming tasks, from simple scripts to complex applications.

10
Chapter 1

Day 1: Installing the


tools

Welcome to this first day of discovering Python! Before diving into


the code, let’s start with the essentials: installing the essential tools
for developing in Python.

1.1 Python Installation: An All in One


Solution
To configure development tools in Python, several options are
available to you. However, we recommend the most efficient and

11
CHAPTER 1. DAY 1: INSTALLING THE TOOLS 12

quick solution: using the PySchool distribution, a free and open


source package. PySchool is equipped with a wide range of tools
aimed at simplifying the Python development process, including:
PySchool Components
1. The latest version of the Python language
2. Integrated Ide: IDLE, Thonny Ide, VSCode, Jupyter Notbook...
3. The Qt Designer visual graphics tool
4. Python libraries necessary for machine learning and artificial
intelligence.
5. As well as many other tools that you will discover!
PySchool package download link: https://sourceforge.net/
projects/pyschool3/

1.2 First Python Program


1.2.1 First Python Program Using the Integrated
IDLE IDE

Python comes with an integrated development environment (IDE)

called IDLE by default. To launch it, simply type IDLE in the search

area of the start menu:


CHAPTER 1. DAY 1: INSTALLING THE TOOLS 13

Once launched, you will see the following view of the user interface

of the Python-integrated IDE. It is a very minimalist IDE, but it is an

excellent tool for beginners to get started and understand the workings

of Python:
CHAPTER 1. DAY 1: INSTALLING THE TOOLS 14

As you observe, the IDLE interface presents a clean and straight-

forward layout, with a text editor window where you can write and edit

Python code. Below the text editor, there is a Python shell window

where you can interactively execute Python code and see the results

immediately. Let’s now have fun defining two integer variables, x

and y, and displaying their sum, z = x + y:


CHAPTER 1. DAY 1: INSTALLING THE TOOLS 15

The IDLE tool can also be used as a calculator. You can utilize

it to perform various mathematical operations, including addi-

tion, subtraction, multiplication, and division, among others.

This capability makes it convenient for quick calculations and ex-

perimentation with numerical expressions:


CHAPTER 1. DAY 1: INSTALLING THE TOOLS 16

1.2.2 First Python program using the Thonny IDE


As mentioned above, the PySchool distribution includes a num-
ber of tools by default, such as integrated development environments
(IDEs) like IDLE, Thonny IDE, VSCode, Jupyter Notebook,
among others. We can use the Thonny IDE to create our first Python
program. Here are the steps to follow :

Step 1 : Launch Thonny IDE from the Start menu :


CHAPTER 1. DAY 1: INSTALLING THE TOOLS 17

Step 2 : Next, enter the following code and save the file :
 
1 s = " H e l l o World ! "

2 print ( s )
 
Where :
1. We defined a variable named s with the value ’Hello World !’
2. We used the print() statement to display the value of the
variable s.
Step 3 : Running the code

Now, click on the icon shaped like a small green triangle loca-

ted on the toolbar.


CHAPTER 1. DAY 1: INSTALLING THE TOOLS 18

You will then see the message ’Hello World !’ appear on the
console ! This is the value of the variable s displayed using the
print() statement.
Chapitre 2

Day 2 : Variables and


Operators

2.1 Variables in Python


Variables play a crucial role in storing the results of our calcu-
lations. In essence :

Python Variable
A variable boils down to a name assigned to a value, al-
lowing Python to memorize it (not delete it) and retrieve
it later by its name. One can conceptualize the variable as a
kind of label affixed to our value, indicating how it is called. In
Python, assigning a value to a variable is done using the =
operator. On the left, :

 
1 myvariable = value
 
1. myvariable : here we specify the name of the variable, which
must be a sequence of characters without spaces.
2. value : here we specify the value of the variable, which can
take the form of any expression, as mentioned earlier.

19
CHAPITRE 2. DAY 2 : VARIABLES AND OPERATORS 20

3. Unlike other programming languages : Python does not


require declaring a variable’s type. A variable is created at the
moment you assign it a value. The type will be automatically
detected during assignment.
The main types of variables in Python are :

Type of Python variable


1. Type integer or int like as : 5 , 7 , 23 ...
2. Type flot or floating point decimal type variable like
12.5 , 6.75 , 1.25 ...
3. Type string like as ’Python’ , ’Java’...
4. Boolean type which takes only two values True or
False

Exemple. variables of integer type


 
1 n = 11

2 print ( n )

3 # ou tp ut : 11
 

After execution this program displays 11. We can also display ex-
planatory text together with the variable n :
Exemple. display the variable with text :
 
1 n = 11

2 print ( " The v a l u e o f n i s : " , n)


 

The program displays at runtime :


The value of n is : 11
Remarque. To display the type of a variable, we use the type(variable_name)
instruction.
Exemple. displaying thevariable type
CHAPITRE 2. DAY 2 : VARIABLES AND OPERATORS 21

 
1 x = 13

2 y = 12.75

3 s = " Learn Python "

4 b = 10 < 3

5 print ( type ( x ) ) # d i s p l a y : <c l a s s ’ i n t ’>

6 print ( type ( y ) ) # d i s p l a y : <c l a s s ’ f l o a t ’>

7 print ( type ( s ) ) # d i s p l a y : <c l a s s ’ s t r ’>

8 print ( type ( b ) ) # d i s p l a y : <c l a s s ’ b o o l ’>


 

Remarque. A Python variable always has a type, even if it is unde-


clared. the type is defined when the variable is introduced and can
be changed subsequently, which justifies the dynamism and power of
the Python language.

Exemple. Type of variable.


 
1 x = 3 # x i s o f type i n t

2 print ( x ) # ou tp ut : 3

4 # change t h e t y p e o f x t o f l o a t

5 x = f l o a t ( x ) # x i s now o f t y p e f l o a t

6 print ( x ) # ou tp ut : 3 . 0
 

2.2 Comments in Python


2.2.1 What is a comment in Python ?

You might also like