Python Programming Fundamentals
Types, Expressions, Strings, Loops, Functions
Dr. Zia Ul Rehman
Anaconda
[Link] [Link]
Arithmetic Operator Comparison Operator Logical Operator
+ < and (return first false
value; if not found
returns last)
- <= or (returns first true
value; if not found
returns last)
* > not (flips the Boolean Operators
value of operand)
in Python
/ >=
Return Type Depends == And, or returns one of
on Operand the operands
!= Not returns only True or
False
Return only True and
False
How Variables Works in Python
• Naming Rules
i=5 • Case sensitive
• Cannot Start with Special
j=i Character except underscore _
• Cannot start with a number
j=3
print(i)
• Integer Numbers – int
• 5, -6
• Decimal Numbers – float
• 3.5, -7.6
Data Types in Python
• Boolean Values – bool
• True, False
• Strings – str
• “Python”
An Informal Introduction To Python
• Input and output are distinguished by the presence or absence of
prompts (>>> )
Using Python as a Calculator
• The interpreter acts as a simple calculator: you can type an
expression at it and it will write the value.
• Expression syntax is straightforward:
• the operators +, -, * and / work just like in most other
languages
• parentheses (() ) can be used for grouping.
Using Python as a Calculator: Numbers
• Division (/) always returns a float.
• To do floor division and get an integer result (discarding any fractional
result) you can use the // operator;
• to calculate the remainder you can use %:
Using Python as a Calculator: Numbers
• With Python, it is possible to use the ** operator to calculate powers.
• The equal sign (=) is used to assign a value to a variable. Afterwards,
no result is displayed before the next interactive prompt:
• If a variable is not “defined” (assigned a value), trying to use it will
give you an error:
Using Python as a Calculator: Numbers
• There is full support for floating point; operators with mixed type
operands convert the integer operand to floating point:
• In interactive mode, the last printed expression is assigned to the
variable _
• This means that when you are using Python as a desk calculator, it is
somewhat easier to continue calculations, for example:
Strings
• Besides numbers, Python can also manipulate strings, which can be
expressed in several ways.
• They can be enclosed in single quotes ('...' ) or double quotes ("...")
with the same result2. \ can be used to escape quotes:
Strings