1python1 AA
1python1 AA
https://www.python.org/shell/
google colab
Script mode:
https://repl.it/languages/python3
https://www.onlinegdb.com/online_python_interpreter
Hello world
Output
Escape Sequence
Using ‘%’ for string formatting
%f for float
11
x = 18
x+9
y=x+x
y+x
Python Variable Name Rules
• Must start with a letter or underscore _
• Case Sensitive
• more@ = 1000000
http://en.wikipedia.org/wiki/Mnemonic
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)
hours = 35.0
What are these bits rate = 12.50
of code doing? pay = hours * rate
print(pay)
Numeric Expressions
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use “computer-speak” to express the - Subtraction
classic math operations
* Multiplication
• Asterisk is multiplication / Division
• Exponentiation (raise to a power) looks ** Power
different than in math
% Remainder
Numeric Expressions
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5 Operator Operation
>>> print(xx) >>> print(kk)
+ Addition
4 3
>>> yy = 440 * 12 >>> print(4 ** 3) - Subtraction
>>> print(yy) 64 * Multiplication
5280
>>> zz = yy / 1000 4R3 / Division
3
Order of Evaluation
• When we string operators together - Python must know which one
to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:
• Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right 11
What Does “Type” Mean?
• In Python variables and constants
have a “type” >>> ddd = 1 + 4
>>> print(ddd)
• Python knows the difference between 5
an integer number and a string >>> eee = 'hello ' + 'there'
>>> print(eee)
hello there
• For example “+” means “addition” if
something is a number and
“concatenate” if something is a string
concatenate = put together
Type Matters
• Python knows what “type” >>> eee = 'hello ' + 'there'
everything is >>> eee = eee + 1
Traceback (most recent call last):
File "<stdin>", line 1, in
• Some operations are <module>TypeError: Can't convert
prohibited 'int' object to str implicitly
>>> type(eee)
• You cannot “add 1” to a string <class'str'>
>>> type('hello')
<class'str'>
• We can ask Python what type >>> type(1)
something is by using the <class'int'>
type() function >>>
Several Types of Numbers
>>> xx = 1
• Numbers have two main types >>> type (xx)
<class 'int'>
- Integers are whole numbers: >>> temp = 98.6
-14, -2, 0, 1, 100, 401233 >>> type(temp)
<class'float'>
- Floating Point Numbers have >>> type(1)
decimal parts: -2.5 , 0.0, 98.6, 14.0 <class 'int'>
>>> type(1.0)
<class'float'>
>>>
Type Conversions
>>> print(float(99) + 100)
• When you put an integer and 199.0
>>> i = 42
floating point in an expression,
>>> type(i)
the integer is implicitly
<class'int'>
converted to a float >>> f = float(i)
>>> print(10 / 2)
Integer division produces a floating 5.0
>>> print(9 / 2)
point result
4.5
>>> print(9 // 2)
4
String
>>> sval = '123'
>>> type(sval)
<class 'str'>
Conversions
>>> print(sval + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
• Why comment?
width = 13
height =8.0
what will be the output of the follwoing
•width // 2 //: qoutient of division but decimal part neglected
•width / 2.0
•height / 5
•3+2*3%5*2**2
Exercise 3
E.g.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Exercise 4
• Write a program which prompts the user for a Celsius
temperature, convert the temperature to Fahrenheit, and
print out the converted temperature
• Operator precedence
Acknowledgements / Contributions