0% found this document useful (0 votes)
4 views4 pages

Python Variables and Conditionals Guide

The document provides an overview of variables in Python, including their definition, creation, and basic types such as integers, floats, strings, and booleans. It also covers advanced usage like dynamic typing, multiple variable assignment, and conditional execution with basic and advanced statements, including logical operators and nested conditionals. Examples are provided for each concept to illustrate their usage in Python programming.

Uploaded by

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

Python Variables and Conditionals Guide

The document provides an overview of variables in Python, including their definition, creation, and basic types such as integers, floats, strings, and booleans. It also covers advanced usage like dynamic typing, multiple variable assignment, and conditional execution with basic and advanced statements, including logical operators and nested conditionals. Examples are provided for each concept to illustrate their usage in Python programming.

Uploaded by

akpevwe4ever
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Variables in Python: Basics

Definition: Variables are containers for storing data values in memory.

Creation: In Python, variables are created the moment you first assign a value to them
using the assignment operator (=).

Basic Variable Types:


Integer (int): Whole numbers, e.g., 5, -10, 0
Float: Decimal numbers, e.g., 3.14, -0.5, 2.0
String (str): Text, e.g., "Hello", 'Python'
Boolean (bool): True or False

# Variable assignment examples


age = 25 # Integer
price = 19.99 # Float
name = "Alice" # String
is_student = True # Boolean

# Checking variable types


print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(is_student)) # <class 'bool'>
Variables in Python: Advanced Usage
Dynamic Typing: Python determines variable types at runtime, allowing flexibility in
variable usage.

# Dynamic typing example


x = 10 # x is an integer
print(x) # Output: 10
x = "Python" # x is now a string
print(x) # Output: Python
x = [1, 2, 3] # x is now a list
print(x) # Output: [1, 2, 3]

Multiple Variable Assignment: Python allows assigning values to multiple variables in a


single line.

# Multiple assignment
a, b, c = 5, 3.2, "Hello"
print(a) # Output: 5
print(b) # Output: 3.2
print(c) # Output: Hello

# Swapping variables (without temporary variable)


x, y = 10, 20
print(f"Before swap: x = {x}, y = {y}")
x, y = y, x
print(f"After swap: x = {x}, y = {y}")
Conditional Execution: Basics
Definition: Conditional statements allow your program to execute different blocks of code
based on whether certain conditions are met.

Basic Conditional Statements:


if: Executes code if condition is True
elif: Checks additional conditions
else: Executes when all conditions are False

# Basic if statement
age = 18
if age >= 18:
print("You are an adult")

# if-else statement
temperature = 25
if temperature > 30:
print("It's hot outside")
else:
print("It's not too hot")

# if-elif-else statement
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or below")
Conditional Execution: Advanced
Logical Operators: Used to combine multiple conditions.
and: True if both conditions are True
or: True if at least one condition is True
not: Inverts the condition (True becomes False, False becomes True)

# Using logical operators


age = 25
has_license = True

# Using 'and' operator


if age >= 18 and has_license:
print("You can drive")

# Using 'or' operator


is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("It's a day off!")

# Using 'not' operator


is_working = False
if not is_working:
print("Time to relax")

Nested Conditionals: Placing if statements inside other if statements.

# Nested if statements
num = 15

if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
else:
print("Non-positive number")

You might also like