0% found this document useful (0 votes)
2 views

Python Class

The document provides an introduction to Python programming, focusing on variable assignment, data types, and data structures. It explains how to assign variables, the different primitive data types (integers, floats, strings, booleans), and various data structures (lists, tuples, dictionaries, sets). Additionally, it covers control flow with conditions and loops, as well as defining and using functions with different types of arguments.

Uploaded by

nwobodope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Class

The document provides an introduction to Python programming, focusing on variable assignment, data types, and data structures. It explains how to assign variables, the different primitive data types (integers, floats, strings, booleans), and various data structures (lists, tuples, dictionaries, sets). Additionally, it covers control flow with conditions and loops, as well as defining and using functions with different types of arguments.

Uploaded by

nwobodope
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction

to
PYTHON
ASSIGNING A VARIABLE
Assigning a variable in programming refers to the process
of associating a value with a symbolic name (the variable
name) so that the program can refer to and manipulate
that value using the given name. It is essentially storing a
piece of data in a memory location and giving it a label for
convenient reference.

To assign a variable, we need three components:

Variable name: We can decide what we would like to


Variables name our variable but keep in mind it can be short and
A variable is a named storage location descriptive. We will learn about certain naming
used to store data in a program. conventions we must follow in a bit.

It is a way to give a name to a memory Assignment operator: The variable name will be
location, making it easier to reference followed by the assignment operator. The assignment
and manipulate data. operator is an equal sign (=).

Variables are used in various The value: The value we assign to a variable can be
programming tasks, such as numerical or consist of characters, known as strings. In
calculations, storage, and manipulation this part of the lesson, we will only be focusing on using
of data. numerical values
DATA TYPES
Variables in Python hold values of different kinds. Data
types dictate what kind of data a variable can store and, in
some cases, the amount of memory allocated to it.

Understanding data types allows us to make assumptions


about the kind of operations that can be applied to a given
variable.

Python makes use of five primitive variable types:

• Integers
• Floats
• Strings
• Booleans

These data types can be categorized into 3 major groups

1. Numbers
2. Strings (Text)
3. Booleans
1. NUMBERS
This group categorizes all numerical data:

• INTEGERS:
Positive or negative whole numbers with no
fractional components.

Examples: 5, -10, 365.

• FLOAT DATA TYPE:


Represents real numbers with decimal points.

Examples: 3.142, -0.5, 9.8.


2. STRINGS (TEXT)
Strings in Python are sequences of characters
enclosed in quotes, either single (') or double
("). They are versatile and widely used for
representing textual data.

Strings can contain letters, numbers, symbols


and even spaces.

Examples: ”Hello, World!”


‘Python is easy!’
“Password123”
3. BOOLEAN
Booleans are built-in data types capable of
taking one of two possible values: True or
False (interchangeable with the integers 1
and 0).

Booleans control the flow of a program and are


used to make comparisons. Although they are
nicknamed bool in Python, they play an
integral role.

Boolean data types are mostly used with


comparison and logical operators.

Examples: >, <, =.


DATA STRUCTURES
Data structures help organize and store data
efficiently. They define how data is arranged,
accessed, and manipulated in a program.

Understanding data structures allows us to


choose the best way to store and retrieve
information based on specific use cases.

Python provides several built-in data structures,


including:

Lists – Ordered, mutable collections of items.


Tuples – Ordered, immutable collections of items.
Dictionaries – Key-value pairs for fast lookups.
Sets – Unordered collections of unique items.

Each data structure has its own properties,


advantages, and use cases in programming.
1. LISTS
Lists in Python are ordered collections of
items, enclosed in square brackets []. They
can store elements of different data types,
including numbers, strings, and even other
lists.

Lists are mutable, meaning their elements can


be changed, added, or removed.

Examples:

fruits = ["Apple", "Banana", "Cherry"]


numbers = [10, 20, 30, 40]
mixed = ["Python", 3.14, 42]
2. TUPLES
Tuples are ordered collections of items,
enclosed in parentheses (). Unlike lists, tuples
are immutable, meaning their elements
cannot be changed after creation.

Tuples are useful for storing fixed sets of


values that should not be modified.

Examples:

coordinates = (4.5, 9.8)


colors = ("Red", "Green", "Blue")
info = ("Alice", 25, "Developer")
3. DICTIONARIES
Dictionaries are unordered collections of key-
value pairs, enclosed in curly braces {}. They
allow efficient data retrieval by using keys
instead of index positions.

Dictionaries are mutable, meaning their


contents can be modified.

Examples:

person = {"name": "Alice", "age": 25, "city": "New


York"}

scores = {"Math": 90, "English": 85, "Science": 88}

contacts = {"Alice": "123-4567", "Bob": "987-


6543"}
4. SETS
Sets are unordered collections of unique items,
enclosed in curly braces {}. They do not allow
duplicate values and do not maintain a
specific order.

Sets are useful when you need to store distinct


values and perform mathematical set
operations like union, intersection, and
difference.

Examples:

unique_numbers = {1, 2, 3, 4, 5}
fruits = {"Apple", "Banana", "Cherry"}
mixed_set = {"Python", 3.14, 42}
Conditions, Loops
and Functions
Python
Conditions
if Statement: else Statement
The if statement checks a The else statement runs when
condition. If the condition is the if condition is false.
true, the code inside the if block
runs. Example.
X = 10 x=3

if x > 10: if x > 5:


print("x is greater than 5") print("x is greater than 5")
else:
print("x is not greater than 5")

In this example, since x is Here, since x is not greater


greater than 5, the message "x than 5, the else block runs,
is greater than 5" is printed. printing "x is not greater than
For
Loops
for Loop
A for loop in Python is used to repeat a
block of code a specific number of times
or to iterate over a collection like a list,
tuple, or string Example:
fruits = ["apple","banana",
"cherry"]
for fruit in fruits:
In this example, the for loop goes print(fruit)
through each item in the fruits list
and prints it. The loop runs three
times, once for each fruit.
While
Loops:
while Loop
A while loop repeats a block of code as long as a
condition is true.
EXAMPLE:
count = 0

while count < 5:


print("Counting:",
count)
count = count + 1
In this example, the while loop runs as
long as count is less than 5. Each time
it runs, count increases by 1, and the
loop eventually stops when count
reaches 5.
Function
s
In Python, a function is a block of code that performs a
specific task. You can define a function once and then
call it whenever you need to perform that task.
Functions help you reuse code and make your programs
more organized..

How to Define a Function


You define a function using the
def keyword, followed by the def greet():
function name, parentheses (), E.g
print("Hello,
and a colon :. Inside the world!")
function, you write the code
that will run when the function
is called.
Function
s
Arguments in Python are the values you pass to a
function when you call it. They allow functions to work
with different data and produce different results.

Types of Arguments in Python:

● Positional Arguments.
● Keyword Arguments.
● Default Arguments.
Positional Arguments
These are the most common type of arguments. The
values are assigned to parameters in the order they are
given.

In the example, “name”


is the first argument
and “message” is the
Example: second.
def greet(name, message):
print(f"{message},
{name}!") Calling the function using
positions of the arguments

greet("Alice", "Hello")
Keyword Arguments:
These arguments are passed using the parameter
names. They can be provided in any order.

Arguments are Passed


by their names
Example:
def greet(name, message):
print(f"{message},
Calling the function using their
{name}!")
names (keyword).

greet(name= "Alice", message =


"Hello")
Default Arguments:
You can provide default values for parameters in the
function definition. If an argument is not provided when
the function is called, the default value is used.

In the example, if the function is


called without passing an
argument for “message”, the
Example: default value is used.
def greet(name, message =
“hello”):
print(f"{message}, Calling the function
{name}!") without “message”
argument.

greet(name="Alice")
Remember to share
your learning on
LinkedIn

(Tag @10Alytics)

You might also like