Professional Documents
Culture Documents
4 Sir
4 Sir
●
To avoid printing ‘World’ in a new line & to put a space
between the words
print("Hello", end=" ")
print("World!")
– end is an optional parameter that specifies what Python should do at
the end of a print statement
●
Permits an arbitrary no. of arguments
– Each argument can be of any data type
• e.g. one argument can be string while another can be float
– A single space is automatically put between arguments
• print('I', 'am', 9, 'years', 'old') → I am 9 years old
– If we need another character in place of space
• print('I', 'am', 'fine', sep = '-') → I-am-fine
• print('I', 'am', 'fine', sep = '') → Iamfine
Number formatting
●
Float values have arbitrary number of decimal places
– e.g. 1.1, 3.0000000005, 6.785767299
●
The built-in format() function
– Has the form
• format(value, format_specifier)
• format_specifier can contain a combination of formatting options
– Returns a numeric string version of the value containing a specified no.
of decimal places
• Actual value is unaffected
• format(3.0000000005, '.0f') → '3'
• format(6.785767299, '.3f') → '6.786'
• format(9**99, '.4e') → '2.9513e+94'
• format(9**-9, '.2e') → '2.58e-09'
• format(13402, ',.1f') → '13,402.0'
– Usage
• x = format(1.1, '.2f')
• print(x)
Obtaining input
●
The built-in input() function
– Takes a string argument
• The string is used as a prompt
– On calling the function,
• The prompt is printed
• Waits for input from user
– The user enters the input and presses <Enter>
• The function returns user input as a string
Loc 2 34.78 x
Assignment operator
– Cascaded assignment
• y=x=8
– Simultaneous assignment
• x, y = 5, 7 + 4
– Augmented assignment
• x += 1 → x=x+1
• x /= 2 + 5 → x = x / (2 + 5)
• x //= 7 → x = x // 7
• x **= 2 → x = x ** 2
●
Variable swapping
• tmp = x
• x=y
• y = tmp
– Alternatively
• x, y = y, x
Arithmetic operators
●
Addition (+), subtraction (–), multiplication (*)
• 7 + 2.0
• 9–3
• 4.5 * 0.5
●
Negation (–)
• e.g. –10, –22, –5
●
Exponentiation (**)
• e.g. 10 ** 2 is 100
●
Division, True division or Float division (/)
– The result of a division is always a float
• 7.9 / 2
• 7 / 34.23
• 4.3 / 8.8
• 8/4
Arithmetic operators
●
Truncating division or Floor division (//)
– Result is the greatest integer ≤ quotient
– If the operands are integers, result is an integer quotient
– If even 1 operand is a float, result is a float holding an integer value
• 25 // 10 is 2
• 25.0 //10 is 2.0
• 10.5 // 5.0 is 2
• 11 // 2.5 is 4.0
●
Modulus, Modulo or Remainder (%)
• 25 % 10 is 5
• 10.5 % 5 is 0.5
• 11 % 2.5 is 1.0
●
The divmod(x, y) built-in function
– Returns the results of both floor division and modulo
• Hours = 130 // 60
• Minutes = 130 % 60
• Hours, Minutes = divmod(130, 60)
Expressions
●
Combination of operators & operands that evaluate to a value
• e.g. 4 * (3 + k), 3 + k, 4, k
– When entered in shell mode, its value is displayed
• e.g. if 7 + 2 is entered into Python shell, it displays 9
●
Arithmetic expression
– An expression that evaluates to a number
●
Mixed-type expression
– An expression having operands of different data types
• e.g. 2 + 4.5
– A binary operator can perform its operation only if its operands are of
the same data type
• If they are not, they need to be first converted into the same type
– The operands can be converted in 2 ways
• Implicit or automatic type conversion (coercion)
• Explicit type conversion
Coercion
●
Performed automatically by Python
– Only if the conversion is safe
• A conversion is safe when there is no information loss
– Integer to float conversion is always safe
• e.g. 2 → 2.0
– Float to integer conversion is not always safe
• 3.0 → 3 (Safe)
• 4.5 → 4 (Unsafe)
– If one operand is a float, the other operand is converted to float
• The result is a float
• 2 + 4.5 → 2.0 + 4.5 → 6.5
Explicit type conversion
●
Performed by the programmer
– Done using built-in type conversion functions
• e.g. int(), float(), str(), bool()
– Can be done even if it is unsafe
• float(2) + 4.4 → 6.4 (safe)
• 2 + int(4.4) → 6 (unsafe)
– int() extracts the integer part of its argument
• Doesn’t round off to the nearest integer
• int(4.9) → 4, not 5
– Numeric strings can be converted to numeric type
• "10" → int("10") → 10
• "10" → float('10') → 10.0
• "10.8" → int('10.8') → Error
• "10.8" → float('10.8') → 10.8 → int(10.8) → 10
• "Hello" → float("Hello") → Error
– Numeric type can be converted to numeric strings
• str(-4.5) → '-4.5'
• str(1 + 10 + 100) → '111'
Explicit type conversion
●
The eval() built-in function
– Takes a string as argument
– Evaluates it like a Python expression
– Returns the result of the expression
• eval('7 + 3') → 10
• height = 5.6
• ht = eval(height)
• print(ht) → '5.6'
• Age = input("Enter your age: ")
• age = eval(Age)
• x, y = eval("10, 20 + 12") → x, y = 10, 20 + 12 → x is 10, y is 32
• Ip = input("Enter name and age (separated by comma): ")
• x, y = eval(Ip)
Statements
●
A single complete command
• e.g. print(‘Hi’)
●
Usually a statement is completely contained in a line
●
A statement can span multiple lines if we put ‘\’ at the end of
the line or by using parenthesis
X = •3 + \ is same as X = (3 + is same as X=3+4
4 4)
•
●
To have multiple statements on a single line
– Separate them with ‘;’
• print("Hello World!"); print('Hi World!')