127.0.0.1 Programming fundamentals Programming Basics – Data Types and Variables

In Python, data types define the type of values that can be assigned to variables. Variables, on the other hand, are used to store and manipulate data of different types within a program. Here’s an explanation of data types and variables in Python with examples:

Data Types in Python

Python has several built-in data types, including Numeric (Integer, Float and Complex), Strings, and Booleans

Numeric Types

Numeric types represent numbers and can be categorized into integers, floating-point numbers, and complex numbers.

Example:

# Integer
x = 10
print(x)  # Output: 10

# Floating-point
y = 3.14
print(y)  # Output: 3.14

# Complex
z = 2 + 3j
print(z)  # Output: (2+3j)

Advertisement



String

Strings are sequences of characters and are enclosed in single quotes (‘ ‘) or double quotes (” “).

Example:

message = "Hello, World!"
print(message)  # Output: Hello, World!

Boolean

Boolean represents the truth values True and False, which are used in logical operations and control flow. Example:

is_valid = True
print(is_valid)  # Output: True

Variables in Python

Variables are used to store and reference data values. In Python, variables are dynamically typed, meaning that you don’t need to explicitly specify the data type of a variable. The data type is inferred based on the value assigned to the variable.

Example:

name = "Alice"  # String
age = 25  # Integer
height = 1.65  # Floating-point
is_student = True  # Boolean

print(name)  # Output: Alice
print(age)  # Output: 25
print(height)  # Output: 1.65
print(is_student)  # Output: True

In this example, the variables name, age, height, and is_student are assigned different data types, and Python automatically determines the appropriate data type based on the assigned values.

Variables can also be reassigned to new values of different types:

x = 10
print(x)  # Output: 10

x = "Hello"
print(x)  # Output: Hello

Here, the variable x is initially assigned the value 10 (integer), but it is later reassigned to the value “Hello” (string).

By using appropriate data types and variables, you can store and manipulate different kinds of data in Python programs.

In Python, you can handle user inputs by using the input() function, which allows you to prompt the user for input and retrieve the value entered by the user. Here’s an example of how to handle user inputs in Python:

name = input("Enter your name: ")
print("Hello, " + name + "!")

age = input("Enter your age: ")
print("You are " + age + " years old.")

In this example, the input() function is used to prompt the user for their name and age. The user’s input is stored in the variables name and age, respectively. The input function displays the message passed as an argument and waits for the user to enter a value.

After receiving the user’s input, you can use the retrieved values to perform further operations or display them as desired. In the example, the program prints a personalized greeting message and the user’s age.

Here’s a sample output:

Enter your name: Alice
Hello, Alice!
Enter your age: 25
You are 25 years old.

It’s important to note that the input() function treats user input as a string by default, regardless of the type of data the user enters. If you need to use the input as a different data type (e.g., integer or float), you can perform the necessary type conversion using the appropriate functions such as int() or float().

age = input("Enter your age: ")
age = int(age)  # Convert the input to an integer

print("Next year, you will be", age + 1)

In this modified example, the user’s input for age is converted to an integer using the int() function. Then, the program performs a simple calculation to print the user’s age next year.

Handling user inputs in Python using the input() function allows you to interact with users, collect data, and incorporate user input into your programs dynamically.

Advertisement