Python
Programming
Introduction
What is Python?
● Python is a popular high-level programming language used in various applications.
● Known for its simple syntax, making it easy to learn and use.
● Can be used for basic tasks (e.g., plotting, automation) to advanced tasks (e.g.,
machine learning, web development).
Why Learn Python?
● Easy to read and write (beginner-friendly).
● Large community and plenty of resources.
● Cross-platform (works on Windows, macOS, Linux).
● Used by top companies (Google, Netflix, NASA, etc.).
Python in the Real World
● Web Development → Django, Flask
● Data Science & AI → Pandas, TensorFlow, Scikit-learn
● Automation → Scripts, bots
● Game Development → Pygame
● IoT & Robotics → Raspberry Pi, MicroPython
Key Features of Python
● Interpreted → No need to compile before running
● Dynamically Typed → No need to declare variable types
● Extensive Libraries → Thousands of pre-built packages
● Open-source & Free
Variables
● Variables are names that store values in a program
● Think of them as containers or labeled boxes
● They make data reusable and easy to work with
Creating Variables in Python
● No need to declare type
● Use = to assign a value
● Example:
name = "Alice"
age = 20
height = 5.6
Variable Naming Rules
● Must start with a letter or underscore _ name or _name
● Can’t start with a number = 1name
● No spaces (use _ instead) = first_name
● Case-sensitive (Age ≠ age)
● Camel case - lastName
● snake case - lastname
Changing Variable Values
● Variables can be updated anytime
● Example
x = 10
x = 25
Multiple Variables
● Assigning same value:
a=b=c=5
● Assigning different values:
x, y, z = 1, 2, 3
Basic Syntax Rules
● Function Syntax
○ def...: indicates that you are defining a new function.
○ function() refers to the name of your function. By convention, this name is typically lowercase and represents a verb/action.
○ a,b refers to parameters (values or variables) that can be used within the statements of your function’s definition (......). If your function has no
parameters, an empty parenthetical () is used.
○ The return statement is an optional statement that will return a value for your function to your original call.
def function(a, b):
......
return a + b
Basic Syntax Rules (cont.)
● Calling a function
○ Call the function by referring to its name (function()) and by placing
any necessary arguments (1, 2) within the parenthesis separated by
commas. myValue = function(1, 2)
○ If you wish, you can set your function call equal to a variable (myValue). The value returned by the
function will be assigned to your variable name.
myValue = function(1, 2)
Common Data Types and Operators
● A data type is a means of classifying a value and determining what operations can be performed
on it. All objects have a data type.
● Operators are symbols used carry out specific functions/computations.
● [Link]
Input/Output
● Input functions (input()) allow users of a program to place values into
programming code.
○ The parameter for an input function is called a prompt. This is a string (this
can be indicated by “” or ‘’) such as “Enter a number: “ xString = int(input(“Enter a number: “))
y=xString+2
○ The user’s response to the prompt will be returned to the input statement call print(y)
as a string. To use this value as any other data type, it must be converted with
another function (int()).
● Print functions (print()) allow programs to output strings to users on a given
interface.
○ The parameter of this function is of any type. All types will automatically be
converted to strings.
If-else Statements
● If-else statements allow programmers to adapt the function of their code based on
a given condition.
● If a given condition (i.e. x % 2 == 0) is true, then the statements following the if
xString = input(“Enter a number: “)
statement (if) will be executed. If the condition is false, the statements following x = int(xString)
the else statement (else) will be executed.
if x % 2 == 0:
print(“This is an even number”)
○ The condition is tested using the Boolean operators == (is equal to), != (is elif x == 0:
not equal to), and (used to test multiple conditions), and or (used to test if print(“This number equals 0”)
AT LEAST ONE condition is true). else:
print(“This is an odd number”)
○ Additionally, else-if statements (elif) can be used to provide unique coding
statements for multiple conditions.
For Loops
● For loops perform the same task (iterate) for the number of times specified
by an iterable (something that can be evaluated repeatedly such as a list,
string, or range).
● for defines the for loop
● x is the variable defining the number of times the statements within the myString = input(“Enter a number: “)
myInt = int(myString)
loop (print(myInt)) are executed.
● The range(start, stop, step) function is often used to define x. for x in range(0, 5, 1):
print(myInt)
○ The starting value is defined by start, the final value is defined by
stop – 1, and the magnitude at which x changes between loops is
defined by step.
● in is a Boolean operator that returns true if the given value (x) is found
within a given list, string, range etc.
While Loops myString = input(“Enter a number: “)
myInt = int(myString)
● While loops are statements that iterate so long as a given Boolean x=0
condition is met. while x < 5:
print(myInt)
○ x (the variable determining whether or not the condition is x= x +1
met) is defined and manipulated OUTSIDE of the header of
the while loop (while)
○ The condition (x < 5) is a statement containing a Boolean
variable.
○ break is a statement used to exit the current for/while loop.
○ continue is a statement used to reject all statements in the
current for/while loop iteration and return to the beginning
of the loop.