## Tuesday, June 30, 2020

#### Exercise 64 ||  Solution

Write two Python functions  quotient() and remainder() which takes two numbers a and b as parameters such that :
1. The function quotient() return the quotient q of the Euclidean division of a by b (without using the operator '//' )
2. The function remainder() return the Euclidean division of a by b (without using the operator '%'

#### Exercise 65 || Solution

Write a function in Python which takes as argument a tuple (a, b) composed of two integers and returns it the greatest common divisor GCD of a and b without using any predefined function in python.

#### Exercise 66 || Solution

Write a function in Python which takes as argument a tuple (a, b) composed of two integers and returns it the least common multiple LCM of a and b without using any predefined function in python.

#### Exercise 67 || Solution

Write a function in Python which takes as argument an integer n and which returns True if the number n is prim and False if n is not prim without using any predefined function.

#### Exercise 68 || Solution

Write a Python function that takes as argument an integer n and that return the list of all divisors of n.

#### Exercise 69 || Solution

Write an algorithm as a function in Python which takes two integers a and b as arguments and returns True if the numbers a and b are coprime and False if not.

#### Exercise 70 || Solution

Write an Python algorithm which asks the user to type a coprime intgeger n and m and returns a tuple (u, v) verifying: um + vn = 1 (Bezout identity)

#### Exercise 71 || Solution

Determine the list of odd divisors of the number 3570 which are multiples of 3 and contained in the interval [500, 2500]

#### Exercise 72  || Solution

Write an algorithm as python function which takes as parameters an integer n and which returns the last digit of n.

#### Exercise 73  || Solution

Write an algorithm as a python function that takes as parameters an integer n and which returns the list of divisors d whose last digit is equal to 1. Test your algorithm for n = 727821.

#### Exercise 80 || Solution

Write an algorithm in Python as a function which takes two numbers m and n as parameters (m < n) and which returns a list formed of all the prime numbers between m and n. Example for m = 10 and n = 20 the function must return
``````[11, 13, 17, 19]
``````

#### Exercise 81 || Solution

Create a Python algorithm that calculates the number of ways to pay 10 Euros, using the 1 Euro, 2 Euro and 5 Euro coins.

Younes Derfoufi
my-courses.net

## 1 - About the Django models

In order to organize the storage data for your app, django offers you the tool called model (django model).
1. A model shelters in the form of classes the names of the tables and fields (attributes) of the storage data.
2. Each model corresponds to a single database table.
3. Each model is a Python class that inherits from django.db.models.Model.
4. Each attribute of the model represents a database field.

## 1. What is a django application?

So far, we've only seen one procedure for creating a Django project. Now, in this section, we will create an application inside the created project. You may be wondering, what is the difference between a django project and a django application? The difference between the two is that a project is a collection of configuration files and applications, while the application is a web service that is added to your django project.

## Wednesday, June 17, 2020

#### Exercise 61

Write a program in Python allowing to delete multiple spaces in a text file named myfile.txt which contains a text: T = 'Python        is         programming         language'

#### Solution

``````
import os
# opening file in read mod
file = open("myfile.txt" , "r")

# retrieving the file content in string type
file.close()
# converting the string content to a list
L = content.split()

# opening the file in write mod by overwriting its content
file = open("myfile.txt" , "w")

# browsing through list items
for word in L:
file.write(word + " ")
file.close()
os.startfile('myfile.txt')
``````

Output:

Younes Derfoufi
my-courses.net

Django provides an integrated administration module that can be used to perform CRUD (create, read, update, delete) operations on models and applications on the django site. It provides a quick interface where the user can manage the content of the application (sometimes called SCRUD with an “S” for search). It is an integrated module designed to perform user-related administration tasks. Let's see how to activate and use the Django administration module (interface). But you have no possibility to access it, you must for that: To see an overview of the admin area of your django site, start the server and type in your browser: http://127.0.0.1:8000/admin/

## Tuesday, June 16, 2020

#### Exercise 60

Write a Python program that allows you to count the frequency of repetition of each word found in a given file.

#### Solution

``````# opening the myfile.txt in read mod
file = open("myfile.txt" , 'r')

# obtaining the myfile.txt content

# closing the txt file
file.close()

# converting the content on python list
L = content.split()

# creation of an empty list which will contain unique elements
uniqueList = []

# browsing through list items
for word in L:
if word not in uniqueList:
uniqueList.append(word)
print("Frequency of : " + word + " : " + str(L.count(word)))
``````

#### Output example

If the content of myfile.txt is
content = 'Python is programming language. Python is object oriented programming language.'
the output will be:
Frequency of : Python : 2
Frequency of : is : 2
Frequency of : programming : 2
Frequency of : language. : 2
Frequency of : object : 1
Frequency of : oriented : 1

Younes Derfoufi
my-courses.net

## 1 - Creation of the django project

If you are using Django for the first time, you will have to take into account the initial configuration. In particular, you will need to automatically generate code that establishes a Django project, i.e. a set of parameters for an instance of Django, including database configuration, Django-specific options, and application-specific parameters.
From the cmd command , go to the directory where you want to store your code, then run the following command:
``````django-admin startproject mysite
``````
This will create a mysite directory in your current directory. Here is an overview of the content of your site that you have just created with the django-admin startproject command:

1. The mysite /: root directory designates the container for your project. It encompasses all of the content on your site.
2. manage.py: contains code that allows you to interact with your Django project via the command line utility.
3. The internal mysite /: directory is the current Python package for your project. Its name is the name of the Python package that you will need to use to import anything inside of it.
4. mysite /__ init__.py: empty file telling Python that this directory should be considered as a Python package.
5. mysite /settings.py: contains the configuration parameters for your Django project.
6. mysite /urls.py: contains the url addresses of your project. Each new url must be declared here in this file.
7. mysite /wsgi.py: an entry point for WSGI compatible web servers necessary for the implementation of your project.

## 2 - Starting the Django server for your project

To verify that your Django project is working correctly, go to the external mysite directory of your application, and run the following command:``` ```
``````python manage.py runserver
``````
You will see the following output on the command line:
Which tells you that the server is started at http://127.0.0.1:8000

## 3 - Change the server port 8000

The server chooses port 8000 by default! But it sometimes happens that this port is occupied by other processes, and that you should change it, it is enough for this to add the port number of your choice just after the runserver command:

#### Example: changing the port to 5050``` ```

``````python manage.py runserver 5050

``````
In this case the server will be accessible at the address: http://127.0.0.1:5050

#### Server shutdown command

To stop the server, just type the combination key: Ctrl + C

Younes Derfoufi
my-courses.net

## 0 - About Python Django Framework

Django is an open source web development framework in Python. It aims to make web 2.0 development simple and fast. For this reason, the project has the slogan "The framework for perfectionists with deadlines. " Developed in 2003 for the local newspaper in Lawrence (Kansas), Django was released under the BSD license from July 2005. Several mainstream sites are now based on Django, including Pinterest3 and Instagram, Mozilla...

## 1 - About Python request module

Requests is a python module allowing to use the http protocol in a very simple way! You will discover its power when you want to recover data from a web page at work through a proxy. Because indeed, it really manages everything! Proxies, cookies, ssl, multipart uploads and many other cool things! We offer in this tutorial, some examples of uses of this library.

## Friday, June 5, 2020

#### Exercise 59

Write a Python program that displays the longest word found in a text file.

#### Solution``` ```

``````# opening the existing file in read mod
f = open("monFichier.txt", 'r')

# getting the file content

# converting the content to a list
L = content.split()

# creating of an empty string which will take the value of the longest word
motMax = ""

# browse the elements of the list while comparing their lengths with this one
for mot in L:
if(len(mot))>= len(motMax):
motMax = mot
print("The longest word in the file is : ", motMax)
``````

Younes Derfoufi
my-courses.net

#### Exercise 58

a) Write a program that lists all the folders in the 'C: / Windows' directory
b) write another program which lists all the files in the 'C: / Windows'  directory.
c) Using the getlogin() method, write a program that make the same operations for the user's Desktop directory.

#### Solution

a) List of all folder in the directory 'C:/Windows'
``````# importing the pathlib module
import os
from pathlib import Path

# creating an empty list which will contain all directories in the chosen directory
folders = []

# choose directory
dir = 'C:/Windows'

# creating a path object from the chosen directory
p = Path(dir)

# looping through the chosen path directory and testing if the entry is directory
for entry in os.scandir(p):
if entry.is_dir():
folders.append(entry)
for rep in folders:
print(rep)
``````
b)List of all file in the directory 'C:/Windows'
``````# importing the pathlib module
import os
from pathlib import Path

# creating an empty list which will contain all directories in the chosen directory
fileList = []

# choose directory
dir = 'C:/Windows'

# creating a path object from the chosen directory
p = Path(dir)

# looping through the chosen path directory and testing if the entry is file
for entry in os.scandir(p):
if entry.is_file():
fileList.append(entry)
for file in fileList:
print(file)
``````
c)List of all folder and all file in the desktop directory
``````# importing the pathlib module
import os
from pathlib import Path

# creating an empty list which will contain all directories in the chosen directory
fileList = []
directoryList = []

# desktop directory
dir = 'C:/Users/' + os.getlogin() + "/Desktop"

# creating a path object from the chosen directory
p = Path(dir)

# testing if the entry is file or directory
for entry in os.scandir(p):
if entry.is_file():
fileList.append(entry)
else:
directoryList.append(entry)
print("File list in chosen directory :")
for file in fileList:
print(file)
print("Folder list in chosen directory :")
for directory in directoryList:
print(directory)
``````

Younes Derfoufi
my-courses.net

## Wednesday, June 3, 2020

#### Exercise 57

Given a file called myfile.txt which contains the following text:
"Python is object oriented programming language".
Write a program in Python that transforms the content of the file by writing each word in a separate line.

#### Solution

``````# opening file in read mode
f = open("myfile.txt" , 'r')

# getting the file content

# converting the content to a list
L = content.split()
f.close()

# opening file in write mode by overwriting on its content
f = open("myfile.txt" , 'w')

# building the contnent file
for word in L:
f.write(word + "\n")
f.close()
``````

Younes Derfoufi
my-courses.net

#### Exercise 56

Given a file called myfile.txt which contains the following lines:

line 1
line 2
line 3

write a Python program that transforms the content into the form:

line 3
this line has just been inserted via Python code
line 2
line 1

#### Solution

``````
# creating and opening file in write mode
f = open('myfile.txt' , 'r')

# getting the file content
f.close()

# converting the content to a list
L = content.split()

# swap the line 1 with the  line 3
swap = L[0]
L[0] = L[2]
L[2] = swap

# Insert new line
L.insert(1, "this line has just been inserted via Python code")

# opening myfile.txt in write mode by overwriting its content.
f = open('myfile.txt' , 'w')

# building file content
for line in L:
f.write(line + '\n')

f.close()
``````

Younes Derfoufi
my-courses.net

## Tuesday, June 2, 2020

#### Exercice 54

1) Write a Python program that create a txt file called myfile.txt and write on it the texte: "Python is object oriented programming language".
2) Write an ohter python program that removes the 3rd word from this file.
Question1
``````
# creating and opening file in write mode
f = open('myfile.txt' , 'w')
f.write("Python is object oriented programming language")
f.close()
``````
Question2
``````
# open file in read mode
f = open('myfile.txt' , 'r')

# getting file content
f.close()

# converting content to a list
L = content.split()

# removing the 3rd element
L.pop(2)

# opening file in write mode by overwriting the existing content
f = open('myfile.txt' , 'w')

#building file content
for word in L:
f.write(word + " ")

f.close()
``````

Younes Derfoufi
my-courses.net

#### Exercice 54:

Write a program in Python which allows you to insert at the 3rd position of an existing file called myFile.txt, the line "This line was inserted via Python code! " without changing the existing content file.

#### Solution

``````# opening file in read mode
f = open("myFile.txt" , "r+")

# getting content lines
f.close()

# Inserting a line at third position
linesContent.insert(2 , "This line was inserted via Python code!\n")

# opening file in write mode
f = open("myfile.txt" , "w")

#writing new lines content
f.writelines(linesContent)
f.close()
``````

Younes Derfoufi
my-courses.net

#### Exercise 53

Create manually  two files in the same directory:
• python_code.py
• myfile.txt
1) - Create a Python program that writes the following three lines to the myfile.txt file:
First line
second line
third line
2) - Write a Python program on python_code.py that  replace the second line with the line:  "sorry! The content of this line has been changed! "

#### Solution

Question1
``````
# opening file in write mode
f = open("myfile.txt" , "w")

# creating a lines content
lines = ["first line\n" , "second line\n" , "third line\n"]

# writing lines on myfile.txt
f.writelines(lines)
f.close()
``````
Question2
``````
# opening file in read mode
f = open("myfile.txt" , "r")

# getting content lines
f.close()

# changing the second line
linesContent[1] = "sorry! The content of this line has been changed!\n"

# opening file in write mode
f = open("myfile.txt" , "w")

#writing new lines content
f.writelines(linesContent)
f.close()
``````

Younes Derfoufi
my-courses.net

## Monday, June 1, 2020

#### Exercise 52

1.  Write a python program that allowing you to create a directory in the desktop called myDir
2. Write  a Python program allowing you to create a file in the desktop named myFile.txt and write it the following lines:
here is an example of a text file
this file was created with python
we can write on this file
3. Write  a Python program allowing you to moving myFile.txt in the  directory  myDir.

#### Solution

Question1 ``` ```
``````import os

# get user name

#getting the desktop directory
d = "C:/users/" + user + "/desktop/"

# creating directory myDir in the desktop
os.mkdir(d + "myDir/")
``````
Question2 ``` ```
``````import os

# get user name

#creating myFile.txt in the desktop
myfile = "C:/users/" + user + "/Desktop/myFile.txt"

#opening  myFile.txt in writing mode w
f = open(myfile , 'w')

#writing on myFile.txt
f.write("here is an example of a text file\n this file was created with python\n we can write on this file")
f.close()
``````
Question3``` ```
``````user = os.getlogin()

#getting the desktop directory

# moving myFile.txt to myDir directory

os.rename("C:/Users/" +user + "/Desktop/myFile.txt" , "C:/Users/" +user + "/Desktop/myDir/myFile.txt")

``````

Younes Derfoufi
my-courses.net