0% found this document useful (0 votes)
9 views15 pages

Python Variables and User Input Basics

Uploaded by

mmumar.mct
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)
9 views15 pages

Python Variables and User Input Basics

Uploaded by

mmumar.mct
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

Python Programming - Lesson 2

Variables, Data Types & Input


Handling
What are Variables?
 A variable is a container used to store data in memory.
 In Python, you don't need to declare the type.
Example:
name = 'Mustapha'
age = 45
# Assigning values to variables
name = "John Doe" # string variabl
eage = 30 # integer variable
height = 1.75 # float variable
is_admin = True # boolean variable
Examples
# Printing variable values
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Admin:", is_admin)

# Updating variable values


age = 31
print("Updated Age:", age)
Variables
 name, age, height, and is_admin are variables.
 We assign values to these variables using the
assignment operator (=)
 We print the values of these variables using
the print() function
 We update the value of the age variable and
print the updated value
Rules for Naming Variables
 Must start with a letter or underscore (_)
 lowercase_with_underscores:
(e.g., first_name, total_amount).
 Avoid using reserved words (e.g., if, while, class) as
variable names.
 Use meaningful and concise variable names to improve
code readability
 Cannot start with a number
 No spaces allowed
 Case-sensitive (age and Age are different)
Data Type
 A Data type is a classification of data based on
its format, size, and set of values it can hold.
 It determines the type of value a variable can
hold, the operations that can be performed on
it, and the storage method for each data value.
 Data types are used to define the type of data a
variable can hold.
 Examples of data types include integers, floats,
strings, booleans, etc.
Example
 x = 5 # x is an integer variable with data type
'int‘
 y = 3.14 # y is a float variable with data type
'float'
Basic Data Types in Python
int → integers (e.g., 5, 40)
float → decimal numbers (e.g., 3.14)
str → text or characters (e.g., 'Hello')
bool → True or False
User Input
In Python, "user input" refers to the data or
information provided by the user to the program
while it's running
In Python, you can use the input() function to
get user input, and then store and process the
input data as needed.
Examples of User Input
1. Text input: Entering a name, email, or
password.
2. Numeric input: Entering a number, age, or
quantity.
3. Choices: Selecting an option from a menu or
list.
4. Files: Uploading a file or selecting a file path.
Characteristics of User Input
1. Dynamic: User input is provided at runtime,
not at the time of writing the code.
2. Variable: The input value can change each
time the program is run.
3. External: User input comes from an external
source (the user), rather than being
hardcoded into the program
Why Use User Input?
1. Flexibility: Allows the program to adapt to
different user needs and scenarios.
2. Interactivity: Enables the program to respond
to user actions and input.
3. Customization: Allows users to personalize
their experience with the program
name = input('Enter your name: ')
print('Hello', name)
Dynamic User Input
name = input("Enter your name: ")
print("Hello, " + name + "!")
Variable User Input
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
External User Input (File Path)
file_path = input("Enter the file path: ")
try:
with open(file_path, 'r') as file:
print([Link]())
except FileNotFoundError:
print("File not found.")
Converting Input to Integer
num = int(input('Enter a number: '))
print(num + 10)
Exercise
Write a Python program for a simple mechatronics
cooling system.
The program should ask the user to input the
temperature reading from the sensor.
If the temperature is above 30°C, the fan should
turn ON. Otherwise, it should remain OFF. Use the
input() function to get the temperature from
the user.

You might also like