Monday 19 August 2019

Python programming for beginner

Writing a Python program is very easy and close to the human language and logic. Python
comes with minimal syntaxes.
Some mathematical operations:-
Let, variable- a=8,b=2
Addition (+): a + b = 10
Subtraction (-): a – b = 6
Multiplication (*): a*b = 16
Division(/): a/b = 4
Modulus (%): a%b = 0 [Remainder, as a is divided by b]
Power or Exponent (**): a**b = 64
Rounding off (//): Example, 7.5//2 = 3.0, where 7.5/2 = 3.75
Variables in python:-
Name of a variable (or any identifier such as class, function, module or other object) can be
anything combined with alphabets, letters and some symbols in the keyboard (except @, $, %
etc.), beginning with a letter (the upper case or lower case letters from A to Z). The names of
the variables are, of course, case sensitive. The variable names, ‘Aa’, ‘aa’, ‘aA' are all different.
But the names cannot be the words given in the following table, used for system commands
and functions. Those are Python keywords (in lower case letters) reserved for the use by the
system.
Some reserved words:
and, assert, break, class, continue, def, del, elif, else,
except, exec, finally, for, from, global, if, import, in,
is, lambda, not, or, pass, print, raise, return, try,
while, with, yield

Numbers:-
Real or Floating [Examples: 19.25, 0.0, -3.23, 1.9e-20, 3.75e2 ]
 Integer [Examples: 6, -23]
 Complex [Examples: 70j, 1.9j, 7+9j, 9.3e-21j]
 Long [Examples: Any number ends with "l" or "L". 123L]

Input/Output(I/O) Statements:
input -
x = 56
y = -190.8
z = 54
x,y,z = 56,-190.8,54
xx = "name" [Alphabetic characters, put inside quotes]
a = input("Give some value")
output-
print x, y, xx, Aa
print “Some statement”
print 'x = ', x

Reading/ Writing in a File
To Open a file"
f1 = open("filename", "w")                             #"w" for writing
f2 = open("filename", "r")                              #"r" for reading
To write in a file
print >> f1, x, y                                            #Writing data: x, y in file indexed ‘f1’
To Close the file
f1.close()

No comments:

Post a Comment

Thanks for comment.