Sunday 13 November 2016

Print number pyramid in python

Output should be:

Enter the number of rows:5
1  
1  2  
1  2  3  
1  2  3  4  
1  2  3  4  5  


Code: 

row = int(raw_input("Enter the number of rows:"))  # taking input
for i in range (1,row+1):                                             # iterating from 1 to row+1 
    for j in range(i):                                                      # iterating from 0 to i
        print "%d " % (j + 1),                                         # printing value of j in each loop
    print "\n", 

How to print in Python without newline or space?

code:

row = int(raw_input("Enter a number :"))
for i in range (row):
    print i,


Know how to run python code 


Output: 

$ python print_pattern1.py 
Enter a number :5

0 1 2 3 4

Find an input number is odd or even using Python. Simple program

This program will find an input number is odd or even using Python.

code:

x = int(raw_input("Enter a number: "))
if (x%2 == 0):
        print "%d is Even" % x
else:
        print "%d is Odd" % x


Know how to run python code 

Output: 

$ python odd_even.py
Enter a number: 5
5 is Odd

$ python odd_even.py
Enter a number: 54
54 is Odd


How to run python code?

For running a python code you must have python installed in your system.

After that use these following:

1. open a file using "vi" if you are working in linux terminal or "start" in windows followed by filename (eg. hello_world.py).

2. Write your code in that file. NOTE: do not forget the tabs/space for indentation, it's important for a script.

3. close that file.

4. python hello_world.py


That's it. Done.

Wednesday 27 July 2016

What is the magic number in elf header?

A "magic number" is the constant numbers given in file header to identify the format of the file.

In ELF file format the first four bytes in ELF header is usually refer to as magic number which is constant and the values are: 7f 45 4c 46

If we see the ascii value of 45 4c & 46, it is E, L & F.

Hurray! We got the type of the file as ELF using those numbers.

Now, You will say How did u get that number?

Here's the command, my friend  :

$ readelf -h /bin/ls
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                                          ELF64
  Data:                                           2's complement, little endian
  Version:                                       1 (current)
  OS/ABI:                                      UNIX - System V
  ABI Version:                               0
  Type:                                            EXEC (Executable file)
  Machine:                                      Advanced Micro Devices X86-64
  Version:                                        0x1
  Entry point address:                     0x4045a4
  Start of program headers:             64 (bytes into file)
  Start of section headers:               104048 (bytes into file)
  Flags:                                            0x0
  Size of this header:                       64 (bytes)
  Size of program headers:              56 (bytes)
  Number of program headers:        9
  Size of section headers:                 64 (bytes)
  Number of section headers:          28
  Section header string table index: 27

The above is showing us the complete header info of ELF file. 

If you want to see the same in octal format, here is the command and  output:

$ od -c -N 16 /bin/ls
0000000 177   E   L   F 002 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020


So, Magic numbers are few constant number which tells the format of the file.

Sunday 26 June 2016

Input and output in different data types in Python

There are mainly three data types, namely:
int
float
string


Let's see an example of all of them in this small program:

a = raw_input("Enter a: ")                   # Taking input for first num in string form
b = raw_input("Enter b: ")                   # Taking input for second num string form
print "a + b as strings: %s" % a  + b   # + will concate and will show in string
a = int(a)                                              # Converting in to integer
b = int(b)                                              # Converting in to integer
c = a + b                                              # Adding two integer
print "a + b as integers: %d" % c         # Printing  two integer
a = float(a)                                           # Converting in to float
b = float(b)                                           # Converting in to float
d = a/b                                                  # deriding
print "a / b as float : %f" % d               # printing as float







Output:
Enter a: 100
Enter b: 35
a + b as strings: 10035
a + b as integers: 135
a / b as float : 2.857143

Take interger as input in Python / Arithmatic operation in Python

#!/usr/bin/python

num1 = int(raw_input('Enter a num:'))
num2 = int(raw_input('Enter second num:'))

add = num1 + num2
sub = num1 - num2
mul = num1 * num2
div = num1 / num2

print(add)
print(sub)
print(mul)
print(div)


Output:
$ python ./input.py
Enter a num:10
Enter second num:2
12
8
20
5

$ python ./input.py
Enter a num:2  
Enter second num:10
12
-8
20
0