Social Icons

Sunday, March 15, 2020

Exercise 61 

Write a program in Python allowing to delete multiple spaces in a text file.


In order to give an explicit solution, we will create two files:
  2. myFile.txt
in the same directory

  1. In the text file myFile.txt, we will insert a the following text with multiple spaces:
    'Python       is        programming                      language'
  2. In the Python file we will insert the following code:

# Opening file in read mode
file = open('myFile.txt' , 'r')
# retrieve of the file content
content =
# Now we can close the file
# converting the string content to a list type
L = content.split()
# creating the text content with a simple space
s = ''
for x in L:
    s = s + x + ' '
# Opening the file in write mode by overwriting on it
file = open('myFile.txt' , 'w')
# write the new content in the file 
# Now you can open manualy the file to see that all multiple space are deleted

Younes Derfoufi

Sunday, March 8, 2020

1 - Pillow and manipulation of images on a Tkinter window

1.1- The Pillow library

To process images, Python has a module called Pillow. Pillow is currently a successor fork of the PIL (Python Imaging Library) project. It is designed to provide quick access to the data contained in an image, it is endowed with a magic and powerful power for the processing and manipulation of different image formats files such as PNG, JPEG, GIF, TIFF and BMP

1.2- Installation of the Pillow library

The Pillow library can be installed in a very simple way using the pip utility:
pip install pillow

The Entry Tkinter input field is a widget that offers users the possibility of entering or editing text in a single line. If the user wishes to use a multi-lines text, Tkinter offers this possibility via the Text widget.

1 - How to create a an Entry Input Field in Tkinter?

To create a label in a main window or in a parent frame we use the following syntax:
entry_name = Entry(parent_window , options = ...)

Friday, March 6, 2020

Exercise 51

By using the os.rename() method, create a python program to rename an existing file on the desktop  called myfile1.txt to myfile2.txt. (you must create the file called myfile1.txt manually on the desktop)


import os
# retrieve username by using the getlogin() method
user = os.getlogin()
#define file path for the existing file in desktop
file1 = "C:/Users/" + user + "/Desktop/myfile1.txt"

# Rename the file1 
os.rename(file1, "C:/Users/" + user + "/Desktop/myfile2.txt")

Younes Derfoufi

Label widgets are used to display one or more lines of text with a same style, a bitmap, or an image with the ability to customize the appearance  of the text and background..

1 - How to create a Label ?

To create a label in a main window or in a parent frame we use the following syntax:
label_name = Label (parent_window, option, ...)

It's a surprise because we were only expecting it next month. Google has released a preview of Android 11, the new version of its operating system for phones and tablets for developers. Images for Pixel 2, 3, 3a and 4 are already available.

To attribute the cyberattacks, the experts relied on the technical elements revealed by Vault 7, this set of confidential documents that a former CIA employee leaked in 2017 through WikiLeaks.

In a blog note, security researchers at the Chinese company Qihoo 360 have just highlighted the hacking activities ... of the CIA, the American intelligence service, in the Middle Empire.
According to them, Uncle Sam's cyber spies have attacked multiple Chinese industrial sectors during the past eleven years: aviation, scientific research institutes, petroleum, high-tech, etc. Government agencies have also been in their sights. The regions most affected by CIA espionage - "APT-C-39" - are Beijing, Guangdong and Zhejiang.

Several months after its announcement, the macOS version of Messenger is finally available on the Mac App Store.
Over the years, Facebook Messenger has established itself as one of the most popular messaging services in the world. Over a billion people use it every month, which puts the app right behind WhatsApp, also owned by Facebook.
Long opposed to advertising notifications, Apple has just relaxed the rules of its App Store. Fortunately, users retain the right to deactivate them.

For several months, Apple has been regularly accused of violating its own rules. The Californian brand sends promotional notifications to its customers to promote Apple Music, Apple TV + or iCloud, which violates an App Store rule prohibiting the use of notifications for advertising purposes. Rather than reconsider its practices, Apple decided to soften its ban. Developers are now allowed to send advertisements ... if the user agrees.
A new machine learning algorithm analyzes user behavior to detect anomalies. More than 6.6 billion false accounts were detected in the past year.
At Facebook, the fight against fraud and disinformation is increasingly done thanks to artificial intelligence. According to US media reports, the social network has developed a new machine learning model capable of detecting fake accounts. Last year, this software would have removed more than 6.6 billion false identities on the platform. It's enormous.

Researchers have found a nasty bug in an essential component that is at the base of the entire cryptographic chain of trust in Intel systems. This vulnerability allowed to decrypt or modify everything on a machine.

Bad news for Intel chip users. Security researchers at Positive Technologies have discovered a flaw in the Boot ROM of the Converged Security and Management Engine (CSME), a hardware memory component that is the basis of the entire cryptographic chain of trust for Intel computers. Executed at the very start of startup, this software - which is therefore impossible to correct - will generate the Trusted Platform Module (TPM), a special memory area that allows encryption keys to be stored securely.
The CSME is also the cryptographic sponsor of the UEFI firmware (the successor to the BIOS) and of the Power Management Controller. In addition, it implements functions called "Enhanced Privacy ID", which allow authentication of other protection systems on the computer, for example dedicated to securing content protected by copyright (DRM) or financial transactions.

Thursday, March 5, 2020

Exercise 49

  1. Write a Python program that allows you to create a file on desktop called myFile.txt and write on it the content s = 'Python is oriented programming language'. You must at first retrieve the username via the os.getlogin() method.
  2.  write a Python program to read the existing file on the desktop myFile.txt. 


# Question 1
# Importing required module
import os

# Retrieving the user name 
user = os.getlogin()

# creating the file myFile.txt on desktop
file = open("C:/Users/" + user + "/Desktop/myFile.txt", 'w')

# writing on myfile.txt
s = 'Python is oriented programming language'

# Question 2
file = open("C:/Users/" + user + "/Desktop/myFile.txt", 'r')
#read myFile.txt and print its content
# Note: a file called myFile.txt is created on your desktop, you can see it

Younes Derfoufi

Exercise 49  ||  Solution

  1. Write a Python program that allows you to create a file on desktop called myFile.txt and write on it the content s = 'Python is oriented programming language'. You must at first retrieve the username via the os.getlogin() method.
  2.  write a Python program to read the existing file on the desktop myFile.txt.

1 - The configparser module 

The Python language has a module called configparser which allows you to use and manipulate configuration files similar to Windows files of .ini type. The configparser module can be used to manage user-configurable configuration files within an application. The content of a configuration file can be organized into sections containing each of the parameters with associated values. Several types of option values ​​are supported, including integers, floating point values, and booleans.

Wednesday, March 4, 2020

Exercise 44

1 - Define a Circle class allowing to create a circleC (O, r) with center O(a, b) and radius r using the constructor: 

    def __init__(self,a,b,r):         
        self.a = a         
        self.b = b         
        self.r = r
2 - Define a Area() method of the class which calculates the area of ​​the circle.
3 - Define a Perimeter() method of the class which allows you to calculate the perimeter of the circle.
4 - Define a testBelongs() method of the class which allows to test whether a point A(x, y) belongs to the circle C(O, r) or not.


# import pi number of math module
from math import pi 
class Circle:
    # create a constructor with parameters center (a,b) and radius r
    def __init__(self,a,b,r):         
        self.a = a         
        self.b = b         
        self.r = r
    # create Area() method
    def Area(self):
        # compute and return the area of circle
        return pi*(self.r)**2
    # create the Perimeter method
    def Perimeter(self):
        # compute and return the perimeter of circle
        return 2*pi*self.r
    def tetbelongs(self, A):
        if ((self.a - A[0])**2 + (self.b - A[1])**2== (self.r)**2):
            return True
            return False
# Testing the class circle
myCircle = Circle(1,0 , 1)
print("The area of myCircle is : " , myCircle.Area() )
print("The perimeter of myCircle is : " , myCircle.Perimeter() )
A = (1,1)

The output is:
The area of myCircle is :  3.141592653589793
The perimeter of myCircle is :  6.283185307179586

Younes Derfoufi

 1. About Tkinter Widget

To create graphics software, you must add graphic elements called widget to a window. The widgets term means a contraction of windows and gadget We have already seen in the previous chapter, that the Tkinter library is equipped with many widgets: command button, text area, drop-down list ... Today, we are going to see the Button widget which allows you to equip graphical applications with command buttons.
Cerberus malware has a feature that extracts single-use codes and sends them to an external server. Proof that the smartphone is not the best place for strong authentication.

Strong authentication by SMS, as we know, is no longer highly recommended, because hackers have the means to intercept them. This is why banks are gradually abandoning this type of technology in favor of single-use codes generated by mobile applications. But that solution is not necessarily ideal either, as a report by ThreatFabric shows.

Despite the explosion in data consumption and cloud services, the energy expenditure of data centers has been contained since 2010. Google sees itself as a model of its kind. Significant progress has been made in the energy efficiency of data centers. An article in Science magazine even established this week that if the amount of calculation performed in data centers increased by around 550% between 2010 and 2018, the amount of energy consumed would have only increased by 6% during the same period.

Tuesday, March 3, 2020

 1 - What is beautifulSoup ? 

BeautifulSoup is a python library that lets you extract informations from a website, or from an XML document, with a few lines of code.

2 - How to intall BeatifulSoup ? 

Installing beautifulsoup is a fairly simple operation, just use the pip command line utility and type:
pip install beautifulsoup4

Installing beautifulsoup is a fairly simple operation, just use the pip command line utility and type: You must install from the command line via the pip utility, other important modules which are required for the proper functioning of beatifulsoup:
Following a request, the NSA delivered its training material to Python for a developer.
The NSA is often put forward for its cybersecurity capabilities. But like any business, the American agency trains its employees in IT professions and in particular in development. For this, it has specific training modules on certain languages ​​such as Python. The problem is that these courses are not accessible to the general public.

CodinGame asked 20,000 developers about their preferences in terms of programming language, among others. According to the survey, JavaScript, Java, Python, C ++ and C are the most common programming languages. Clojure, F # and OCaml complete the list.

Monday, March 2, 2020

Exercise 43. Bank Account class:

  1. Create a Python class called BankAccount which represents a bank account, having as attributes: accountNumber (numeric type), name (name of the account owner as string type), balance.
  2. Create a constructor with parameters: accountNumber, name, balance.
  3. Create a Deposit() method which manages the deposit actions.
  4. Create a Withdrawal() method  which manages withdrawals actions.
  5. Create an bankFees() method to apply the bank fees with a percentage of 5% of the balance account.
  6. Create a display() method to display account details.
  7. Give the complete code for the  BankAccount class.


class BankAccount:
    # create the constuctor with parameters: accountNumber, name and balance 
    def __init__(self,accountNumber, name, balance):
        self.accountNumber = accountNumber = name
        self.balance = balance
    # create Deposit() method
    def Deposit(self , d ):
        self.balance = self.balance + d
    # create Withdrawal method
    def Withdrawal(self , w):
        if(self.balance < w):
            print("impossible operation! Insufficient balance !")
            self.balance = self.balance - w
    # create bankFees() method
    def bankFees(self):
        self.balance = (95/100)*self.balance
    # create display() method
    def display(self):
        print("Account Number : " , self.accountNumber)
        print("Account Name : " ,
        print("Account Balance : " , self.balance , " $")
# Testing the code :
newAccount = BankAccount(2178514584, "Albert" , 2700)
# Creating Withdrawal Test
# Create deposit test
# Display account informations

The output is :
Account Number :  2178514584
Account Name :  Albert
Account Balance :  2600  $

Younes Derfoufi

1 - Opening and writing to an existing file 

To write in existing a file, you must add one of the parameters to the open() function:
  1. "a" Append - will be added at the end of the file 
  2. "w" Write - will overwrite any existing content 
  3. "r+" Read and write without overwriting existing content
We will then say that the file is open in write mode. To write to in file open in write mode, we use the write() function. The syntax is:

Sunday, March 1, 2020

 Exercice 42

  1. Create a Python class Person with attributes: name and age of type string.
  2. Create a display() method that displays the name and age of an object created via the Person class.
  3. Create a child class Student  which inherits from the Person class and which also has a section attribute.
  4. Create a method displayStudent() that displays the name, age and section of an object created via the Student class.
  5. Create a student object via an instantiation on the Student class and then test the displayStudent method.


class Person:
    # define constructor with name and age as parameters
    def __init__(self, name, age): = name
        self.age = age
    # create display method fro Person class
    def display(self):
        print("Person name : ",
        print("Person age = ", self.age)
# create child class Student of Person class
class Student(Person):
    # define constructor of Student class with section additional parameters 
    def __init__(self, name , age , section):
        Person.__init__(self,name, age)
        self.section = section
    # Create display method for Student class
    def displayStudent(self):
        print("Student name : ",
        print("Student age = ", self.age)
        print("Student section = ", self.section)
# Testing Person class
P = Person("Tomas Wild", 37)
S = Student("Albert", 23 , "Mathematics")

The output is as follow:
Person name :  Tomas Wild
Person age =  37
Student name :  Albert
Student age =  23
Student section =  Mathematics

Younes Derfoufi

1. About a Python files 

Until now, the programs we have carried out have only processed a very small number of data. We could therefore each time include this data in the body of the program itself(for example in a list). However, this way of proceeding becomes completely inadequate when one wishes to process a larger quantity of information. Hence the justification for using the files.

2. File open mode 

 In Python language, there is no need to import a library to read and write files. These are operations managed natively in  language. The first thing to do is to use Python's built-in open() function to get a Pyhon file object. The open() function opens a file in a fairly simple way! When you use the open() function, it returns an object of file object type. File object contain methods and attributes that can be used to collect informations about the file you have opened. They can also be used to manipulate the said file. A file object created by the open() method, has certain properties allowing to read and write in a file. Its syntax is:
f = open([file name], [open mode])
The [file name] is the name of the file you want to open or create. The opening mode includes the following parameters:
  1. 'r' mode: opening an existing file in read-only mode,
  2. The 'w' mode: open for writing only, overwritten if it already exists and creates if it does not exist,
  3. 'a' mode: opening and writing at the end of the file with conservation of existing content
  4. '+' mode: open in read and write
  5. 'b' mode: opening in binary mode

3. Opening and reading a file 

To read an existing file, several methods are available: 3.1. Full read with the read() method The read() method allows you to read the total or partial content of a file, after being opened with the open() method. The syntax is:

Example. open and read an existing file

f = open("myFile.txt", 'r')
content = # reading content
print(content) # print content
f.close() # closing the file

3.2. Partial reading with the read() method 

The read() method can also be used to read part of the file only by indicating the number of characters to read in parentheses: Example. read the first 20 characters
f = open("myFile.txt", 'r')
content = # read 20 characters from file content
print(content) # print content
f.close() # closing the file
Note After executing the read(n) function(n = number of characters to read), the cursor is at position n + 1, and therefore if you execute the function a 2nd time, reading will start from(n + 1 ) th character.

3.3. Sequential reading character by character 

The read() method can also be used to perform a sequential reading character by character using the for loop:
for c in

Example: of sequential read

f = open("myFile.txt", 'r')
s = ""
for c in
    s = s + c
The same operation can be performed using the while loop: Example. reading a file with the while loop
f = open("myFile.txt", 'r')

s = ""
while 1:
    c =
    if c == "":
    s = s + c

3.4. Reading line by line with the readline() and readlines() methods 

3.4.1. The readline() method 

The readline() method allows you to read a file line by line. This method points to the first line when it is executed for the first time, then to the second line when it is executed for the second time and so on the n-th execution it points to the n-th line. Example. read the file line by line
f = open("myFile.txt", 'r')
print(f.readline()) # print line n ° 1
print(f.readline()) # print line n ° 2
By combining the readline() method with the while() method, you can read all of the lines in a file:

Example. read all lines with readline()

f = open("myFile.txt", 'r')
s = ""
while 1:
    line = f.readline()
    if(line == ""):
    s = s + line
print(s) # printing all lines

3.4.2. The readlines() method 

The readlines() method, returns a list whose elements are the lines of the file Example. read lines from file with readlines()
f = open("myFile.txt", 'r')
content = f.readlines()
print(content [0]) # printing the first line
print(content [1]) # print the second line


You can also read all the lines of the file by applying the for loop: Example. read lines using the for loop
f = open("myFile.txt", 'r')
content = f.readlines()
for line in content:
We can therefore, via readlines(), retrieve the number of lines in a file by applying the len() method:

Example. number of lines in a file

f = open("myFile.txt", 'r')
content = f.readlines()
line_number = len(content) # retrieving the number of lines in the file
By retrieving the number of lines in a file, we can therefore read all of its lines using the for loop:

Example. read all the lines with the for loop

f = open("myFile.txt", 'r')
content = f.readlines()
n = len(content)
for i in range(0, n):
    print(content [i])

3.4.3. Reading a file at a specific position with the readlines() method 

The readlines() method also allows us to read a file at a specific position: Example. reading a file from character 10 to character 20 of the third line
f = open("myFile.txt", 'r')
content = f.readlines() [2] # retrieving the second line
result = content [9:19] # extract from the character at position 10 to 20

3.4.4. Reading from a given position using the seek() method 

The seek() method allows you to select a precise position for reading or writing. Example. read the file from the 6 th position
f = open("myFile.txt", 'r') # selection of position 5
print( #read the file from the 6 th position

Younes Derfoufi

Learn Python For Beginners

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (15) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) Github (2) HTML (7) IT News (3) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (36) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (13) Python Reference (1) Python Source Code (3) Python-Books (6) Python-DVD-Training (1) Python-Exercises (297) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) python-temp (3) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (4) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)

Sample text

Sample Text

Blogger Templates