Introduction To Python
Introduction To Python
INTRODUCTION TO PYTHON
INTRODUCTION
WHAT IS Python is a high-level, versatile, and widely-
used programming language known for its
simplicity and readability. It was created by
Language
level details, making it easier to write and understand code.
Interpreted Python is interpreted, not compiled, which means you can write and
run code directly without the need for a separate compilation step.
Language This makes development faster and more interactive.
Multi- Python supports multiple programming paradigms, including
paradigm procedural, object-oriented, and functional programming, giving
developers flexibility in how they approach problems.
Extensive Python comes with a vast standard library that includes modules for
Library
simplifies development.
Open Source and distribute it freely. This openness has contributed to its
widespread adoption.
Development
Python Installation
Python Installation
Installing python
The official Python website is www.python.org, where downloads,
tutorials, community, etc. may be found.
A convenient way of installing Python together with a large
number of packages (several to be used during the course) is to
install Anaconda (www.anaconda.com/download/)
Choose Python 3 (the stable version is currently 3.11), since it will
be assumed on all slides, assignments, etc.
Find some suitable IDE/working environment, e.g., PyCharm,
PyDev, Jupyter, Emacs
Variables and
Strings
Variables and Numbers
A variable is created when a value is assigned to it
v = 3.6
There are three types of numbers; int, float, and complex:
i = 314
f = 3.14e2
z = 2+3j
The type of a variable can be checked with isinstance(...)
b = isinstance(i,float) # b = False
Strings and Castings
Strings (str) are surrounded by single or double quotes
b = isinstance("I",str) # b = True
Casting using constructor functions; int(...), float(...), str(...)
i = int(3.14) #i=3
f = float(3) # f = 3.0
s = str(3.14) # s = "3.14"
f = float(s) # f = 3.14
Operators
Operators
Arithmetic operators; +, -, *, /, ** (exp.), // (floor div.), %(modulus)
v = 2.0 + 2**3 # v = 10.0
Assignment operators; =, +=, -=, *=, /=
x = 12
x += 2 # x = 14
Comparison operators; ==, !=, >, <, >=, <=
b = (2.0 == 2) # b = True
Logical operators; and, or, not
b = (1+1 == 2 and not(4>5)) # b = True
Identity operators; is, is not
b = (2 is 2.0) # b = False
b = (1+1 is 2) # b = True
Control
structures
Control Structures - If Statements
if statements (with elif and else)
if n>5:
print("more than 5")
elif n == 5: # - elif not required,
print("equal to 5") # and multiple allowed
else: # - else not required
print("less than 5") # and most one allowed
Python is a whitespaces sensitive programming language.
They are used Instead of curly braces/brackets
Loops
Loops - For
Examples
for i in range(3): # Prints 0, 1, 2
print(i)
for i in [1,2,3]:
if i % 2 == 0:
continue
print(i) #Prints 1, 3
While Loops
Loops - While
Examples with break and continue
i=1
while i < 4: # Prints 1, 2, 3
print(i)
i += 1
i=1
while i < 4: # Prints 1
if i % 2 == 0:
break
print(i)
i += 1
Loops - While
Examples with break and continue
i=1
while i < 4: # Prints 1 and then
if i % 2 == 0:
continue # enters infinite loop
print(i)
i += 1
CONCLUSION
That was simple!
QUESTIONS?
Exercise
Write a Python program that asks the user to input a number. If the number is even, the program should print "The
number is even." If the number is odd, it should print "The number is odd." Ensure that you store the user's input in a
variable and use a conditional control structure to determine whether it's even or odd.
Create a Python program that calculates the factorial of a given positive integer. Ask the user to input a positive integer,
and then use a loop (e.g., a "for" or "while" loop) to calculate and print the factorial of that number. Make sure to use a
variable to keep track of the result.
Write a Python program that generates the Fibonacci sequence up to a specified number of terms. Ask the user to input
the number of terms they want in the sequence. Use a loop and variables to generate and print the Fibonacci sequence.
For example, if the user enters 5, the program should print "0, 1, 1, 2, 3" as the first 5 terms of the Fibonacci sequence.