Showing posts with label learn python easy. Show all posts
Showing posts with label learn python easy. Show all posts

Tuesday, November 18, 2014

Indentation

Indentation in Python

By default python uses 4 spaces per indentation level


Example:>>>def A1( ):

                     ____x=10

                     ____for i in range(1,10,1)

                     ________print i



Spacing and maximum length of a line is 79.


Do not mix Tabs and spaces.

Python Identifires

 Python Identifiers


Python identifier is a name used to identify a variable,function,class,module or other objects.

There are some rules/conventions about naming identifiers.

  • An identifier can start with the letter “A-Z” or “a-z”. 
  • An identifier can start with the “_”followed by any digit or letter. 
  • An identifier can start with the any letter followed by any digit. 
  • An identifier can’t start with a digit 
  • An identifier can’t use functional characters such as @,%,!

Note: Python is case sensitive language. So “name” and “Name” are 2 different identifiers.

Some conventions about naming identifiers.
  • Class names start with uppercase letters and all other identifiers with a lowercase letter     Ex: Student stu1=new Student( )                                                                                                             Shape s1=new Shape( )
  • Starting an identifier with single leading underscore indicate by convention that the identifier is need to be private                                                                                            Ex: _accountno
  • Starting an identifier with 2 leading underscores indicate a strongly private identifire Ex:_ _password      _ _pinnumber

Monday, November 17, 2014

Key Words in Python

Key Words in Python

Key words are the reserved words that use for specific command.
You can see those key words below.


and
del
form
not
with
as
elif
global
or
while
assert
else
if
pass
yield
break
except
import
print

class
exec
in
return

continue
finally
is
raise

def
for
lambda
try


As python is case sensitive language you should type those keywords in simple letters.
If you are using default settings in python GUI these key words are orange colored or purple colored.




Sunday, November 16, 2014

Introduction & Arithmetic Functions

Introduction to Python

Here we use version 2.7.6.
Let’s take a look at python GUI.



Use of arithmetic and logical expressions.

Order of mathematical operations.

  1. Parentheses : ( ) 
  2. Power : ** 
  3. Multiplication,Division : * , / 
  4. Addition , Substraction : + , - 
  5. XOR : ^ 

Let’s work with some arithmetic problems.
(you can type an expression in python gui then press enter for the result)


1).2+5     2).5/2     3).9/3    4).9.0/3    5).9.0/3.0     6).2**3+8


---------------------------------------------------------------------------------------------------------------


we can use mode function using “%” symbol.

7).5%3 ;(Gives remainder)


N.B. Check out above highlighted ones. They gives different answers.
         # is used to indicate comments in python.
         You do not need to type those comments.




How to calculate complex arithmetic.



example 1 : 20/7*2.0**/1.5


Steps:


First name the symbols according to their simplification order.
(Refer the order mentioned above)


20/7*2.0**3/1.5
                (3)              (3)                              (2)               (3)
  1. If you have same order number in many locations number them from left to right.
    20/7*2.0**3/1.5
                    (3)              (3)                              (2)              (3)
                     (1)              (2)                                                  (3)
  2. Then simplify according to step 1 or if you have to do step to do the calculation from left to right
    20/7*2.0**3/1.5
                    (3)              (3)                          (2)                    (3) highlighted one first
                      (1)              (2)                                                   (3)
    20/7*8.0/1.5
                   (3)               (3)                        (3) highlighted one first
                    (1)               (2)                         (3)
    2*8.0/1.5
                (3)                      (3) highlighted one first
                 (2)                       (3)
    16.0/1.5
                                    (3) highlighted one first
                                     (3)
    10.666666666666666 This is the answer


First try these problems with your mind. Then check out in python gui .


  1. 45/6*5-6 
  2. 5*4*(3+4)+6%4 
  3. 7.0*5+2**4*(8/5+5*(9%4)-3