Wednesday, July 27, 2016

Python tutorial:Doing math with the interactive shell

python programming tutorial math operators
Python tutorial


Python tutorial: First steps with python


Hello friend, welcome to another python tutorial, this time you will make your first steps with python, and you will learn about math operators,
in this tutorial we will work with python IDLE, so let us start
first here are Math Operators:
python programming tutorial  math operators
math operators


Math with python: addition, subtraction, multiplication and division

python IDLE can be a calculator, so enter the following expressions into the interactive shell to see for yourself:
python programming tutorial math interactive shell
math with interactive shell


as you see the interactive shell is a calculator  ^_^ , even if we give it a complicated expression it will calculate it , let us try:
python programming tutorial math interactive shell
complicated math expression for interactive shell


now let us take a look on other operators:
**          exponent
%           Modulus/remainder
//             integer division

to understand each one of them we use examples,

Exponent:

           a**b = a*a*a*...... b time
           3**4 = 3*3*3*3    four times

python programming tutorial exponent interactive shell
exponent


Modulus:

          a%b = the rest of division a/b
          16%7 =2    because 16 = 7 * 2 + 2
python programming tutorial modulus interactive shell
modulus


Integer division :

            a//b= the integer part of the a/b
            7//2=3  because 7/2=3.5 and the integer part is 3
python programming tutorial math integer division interactive shell
integer division


this is the end of this python tutorial, i hope that you enjoyed it and learn something new
don't forget to practice
thanks
if i missed something i will be pleased to hair from you in the comments below


Tuesday, July 26, 2016

Run python code on IDLE, windows CMD and linux terminal

python programming tutorial
execute python code

Hello friends, welcome to another python programming tutorial, today you will learn how to execute your python code
for python IDLE that comes with installation you can create a new file and write your code and choose run from the menu or press F5 to run it
excute python code on Python IDLE
Python IDLE



if you prefer using another text editor you can create a new py file and write your code  and save it
after you will do the following:
On Windows: 
                  -open cmd from start menu or by pressing at the same time (windows button + R) and                            write "cmd" then press Enter
windows + r
windows + r 

                 - change your actual directory to the directory where py'sfile is, using "cd" commande.
                  -write in the commande line:   yourfilename.py
excute python code on windows cmd
windows cmd


On Linux:
                -open terminal
               - change your actual directory to the directory where py'sfile is, using "cd" commande.
                  -write in the commande line:   python yourfilename.py



run python code on linux terminal
run python code on linux (Terminal)







like that your python code will be executed

thanks
if you did not understood something let me know and write it down


Sunday, July 3, 2016

python script to watch up for a file modification

python,script,file modification,useful
welcome to my first python tutorial in this blog
sometimes you would have to know if some import file has been modified so that you can prevent some damage to happen early so in this tuto you will learn how to use python to alert you if a specific file has been modified.
let's start

first we are going to import os and time modules

import os,time

now we are going to ask the user for the path of the specific file

file_path=input("insert the file path")

now we get the size of the file and back_up it to use it later in a while loop

size=os.path.getsize(file_path)
backup_size=size

now we will use a while loop and we will compare the size of file that we actualize every specific
amount of seconds (in this case 2) using time.sleep() function, and print that the file is ok in the actual time using the time.asctime() function.

while size==backup_size:
t=time.asctime()
print("the file is OK at : ",t)
time.sleep(2)

when the while loop breaks, that mean when the size the size is different than backup_size
we print that the file was modified and the time of modification

print("the file was modified at : ",time.asctime())


the final useful script is :

import os,time
file_path=input("insert the file path")
size=os.path.getsize(file_path)
backup_size=size
while size==backup_size:
t=time.asctime()
print("the file is OK at : ",t)
time.sleep(2)
print("the file was modified at : ",time.asctime())

i hope that you find this script useful and that you learned something new from this post
if you didn't understand something you can comment  right down
share it