Sei sulla pagina 1di 6

Introduction To PYTHON USING PHYTHON AS A CALCULATOR? Numbers >>> 3+2 # Addition 5 >>> (50-5*6)/4 5.0 >>> 8/5 1.

6 >>>2**2 # power of 4 >>> pow(2,3) # power of 8 >>>abs(-5) # obtain Absolute 5 >>> 5//3 1 #floor value

value

>>>import math # importing math Module ? explain latter >>>math.floor(2.5) 2 >>math.sqrt(4) 2 # square root of a number

standard data types: Numbers String List Tuple

Python supports four different numerical types: int (signed integers) long (long integers [can also be represented in octal & hexadecimal]) float (floating point real values) complex (complex numbers)

Complex numbers >>> 1j * 1J (-1+0j) >>> 1j * complex(0, 1) (-1+0j) >>> 3+1j*3 (3+3j) >>> (3+1j)*3 (9+3j) >>> (1+2j)/(1+1j) (1.5+0.5j) >>> a=1.5+0.5j >>> a.real 1.5 >>> a.imag 0.5 >>> >>> 3.0 >>> 4.0 >>> 5.0 a=3.0+4.0j a.real a.imag abs(a) # sqrt(a.real**2 + a.imag**2)

# simple Calculation >>> width = 20 >>> height = 5*9 >>> width * height 900 # several variables simultaneously >>> x = y = z = 0 # Zero x, y and z >>> x 0 >>> y

0 >>> z 0 Strings >>> 'spam eggs' 'spam eggs' >>> 'doesn\'t' "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> "\"Yes,\" he said." '"Yes," he said.' >>> '"Isn\'t," she said.' '"Isn\'t," she said.' Multiline Text #text with new line sequence >>>test="this is a sample\ text which contain multiple lines.\n this is the first line\n\ this is the second" print (test)

# raw string
>>>test=r"this is a sample\ text which contain multiple lines.\n this is the first line\n\ this is the second" print (test) #Text will print as it is test = """ Usage: thingy [OPTIONS] -h -H hostname """ print (test) >>> word = 'Apple' + '4' Apple4 >>>Word*5 Apple4Apple4Apple4Apple4Apple4 >>>Apple Knight AppleKnight

Display this usage message Hostname to connect to

>>>Apple+Knight AppleKnight >>>Apple Knight Apple Knight >>>Apple .strip()+Knight AppleKnight >>>Str=ABCDEF 123 >>>Str[0] A >>Str[9] >>>Str[2:8] CEDF 12 >>>Str[2:] CDEF 123 Python not allow assignment like Str[0]=X but C allows Lists list = [ 'apple', 786 , 5.56, 'Orange', 70.2 ] print print print print (list) (list[0]) (list[1:3]) (list[2:]) # Prints complete list # Prints first element of the list # Prints elements starting from 2nd to 4th # Prints elements starting from 3rd element

cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester'] print(cats) ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester'] cats.remove('Tom') #removing from the list print(cats) ['Snappy', 'Kitty', 'Jessie', 'Chester'] cats.pop(0) print(cats) ['Kitty', 'Jessie', 'Chester'] cats.insert(0,'gizzi') print(cats) ['gizzi', 'Kitty', 'Jessie', 'Chester'] cats.append('jerry') print(cats) ['Kitty', 'Jessie', 'Chester', 'jerry']

Tuples tuple = ( 'apple', 786 , 2.23, 'knight', 70.2 tinytuple = (123, 'Orange') print (tuple) print (tuple[0]) print (tuple[1:3]) )

# Prints complete list # Prints first element of the list # Prints elements starting from 2nd to 4th

print (tuple[2:]) # Prints elements starting from 3rd element print (tinytuple * 2) # Prints list two times print (tuple + tinytuple) # Prints concatenated lists

Control Flow Tools If Statement: The syntax of the if statement is: if expression: statement(s) sample code >>> if(2<3): print('2<3') The else Statement: The syntax of the if...else statement is: if expression: statement(s) else: statement(s) sample code >>> if(2>3): print(' This shouldn't happen ') else: print('True!') while Loop: The syntax of the while loop is: while expression: statement(s) sample code >>> a=0 >>> while a < 10: a = a + 1 print(a)

while for: The syntax of the for loop is: for i in range(0,100): print (i) while loop example # cheerleading program word = input("Who do you go for? ") for letter in word:

call = "Gimme a " + letter + "!" print call print letter + "!" print "What does that spell?" print word + "!"

Potrebbero piacerti anche