program img

Upload: danih46

Post on 03-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Program Img

    1/4

    Programimg

    The programming language is going to define sin taxis and semantics

    Low level language: move data from one location to another, program basically level machine.

    High level language: Uses more abstract term, invert a matrix, compute a function

    Compiled language: The abstractions are converted back into low level abstraction, then executed

    Interpreted language: Special program converts source code to internal data structure, then

    interpreter sequentially converts each step into low level machine instruction and executes.

    Python objects.

    Program (or script) is a sequence of definitions and commands.

    Objects.

    Programs will manipulate data objects.

    Each object has a type that defines the kinds of things programs can do to it

    Objects are:

    Scalar

    int: integers, 5,100, 2563 float: real numbers, 2.0, 25.03, 3.14

    Non scalar

    Expressions

    Operators on ints and floats

    Sum +. Int + int = int float+ (int/float) = float

    Difference -

    Product *

    Division / : must be 3.0 one float al least

    **,

  • 8/12/2019 Program Img

    2/4

    =

    == equal

    != not equal

    Boolean VALUES

    A and B is true if both are True

    A or B is true if both are True

    Not A is true if A if false, and false if A is true

    Convert int to float float (3)

    True or false = true

    False or false = false

    Strings. str

    Len = length

    Indexing

    abcd [0] 0 indicates the number that the object occupies in the string, so the number 0 I the

    letter a, 1 is b and so and so..

    abc [-1] returns c. because starts in 0 , and from the last counts -1 , -2 etc.

    Slicing

    If s is a string the expression s[start:end] denotes the substring that starts at star and ends at end -

    1

    "abcd"[2:]

    the string is abcd

    the first element "abcd"[0] is "a"

  • 8/12/2019 Program Img

    3/4

    the third element "abcd"[2] is "c"

    the use of : with nothing after it means "until the end"

    so "abcd"[2:] is going to start with element 3 and continue until the string

    ends.

    Comands

    Print

    Raw_input

    y = float (raw_input('Enter a number: '))

    print (y*y)

    area del rectangulo

    print ('area de un rectangualo')

    base = float (raw_input('base: '))

    altura = float (raw_input('altura: '))

    print (base*altura)

    Branching programs,

    Conditional

    x = int(raw_input('Enter an integer: '))

    if x%2 == 0:

    print('')

    print('Even')

    else:

    print('')

  • 8/12/2019 Program Img

    4/4

    print('Odd')

    print( 'Done with conditional' )

    x = int(raw_input('Enter an integer: '))

    if x%2 == 0:

    if x%3 == 0:

    print('Divisible by 2 and 3')

    else:

    print('Divisible by 2 and not by 3')

    elif x%3 == 0:

    print( 'Divisible by 3 and not by 2' )

    else:

    print ('not divisible by 2 and 3')

    if type(varA) == str or type(varB) == str:

    print ('string involved')

    elif varA == varB:

    print('equal')

    elif varA > varB:

    print('bigger')

    else:

    print('smaller')