Python Basics: Variables, Data Types, and Interactive Programs
Shirish Shankhdhar
Manager, Analytics & Metrics | Data Science MS | Machine Learning | Driving Data-Driven Insights at Mastercard
In the previous article (Getting Started with Python: Installation, Setup, and Your First Program), I covered installing Python and writing your first "Hello, World!" program. Now, I’ll take things further by exploring Python's core building blocks: variables and data types. Understanding these concepts will give you the foundation to write more complex and interactive Python programs.
1. What Are Variables?
A variable in Python is like a container that holds data. You can store different types of information in variables, such as numbers, text, and more.
Here’s how you declare a variable:
name = "John"
age = 30
height = 5.9
Example: Printing Variables
#defining variables
name = "Alice"
age = 25
height = 5.6
#printing variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
Output:
Name: Alice
Age: 25
Height: 5.6
2. Basic Data Types in Python
Python has several built-in data types that you’ll use frequently. The most common ones include:
Here’s an example that demonstrates different data types:
name = "Charlie" # String
age = 29 # Integer
height = 5.8 # Float
is_student = True # Boolean
# Check and print the data types
print(type(name)) # Output: <class 'str'>
print(type(age)) # Output: <class 'int'>
print(type(height)) # Output: <class 'float'>
print(type(is_student)) # Output: <class 'bool'>
In this code, use the type() function to check the data type of each variable.
3. Type Conversion
Sometimes, you’ll need to convert data from one type to another. For example, converting an input string into an integer.
Example: Converting Between Types
age_str = "25" # This is a string
age_int = int(age_str) # Convert string to integer
print("Age (as string):", age_str)
print("Age (as integer):", age_int)
Output:
Age (as string): 25
Age (as integer): 25
Here, we used int() to convert the string "25" into the integer 25. Similarly, you can use str(), float(), or bool() to convert data between types.
领英推荐
4. Working with User Input
One of the most exciting things about programming is making your code interactive. You can get input from the user using the intput() function.
Example: Getting Input from the User
name = input("What is your name? ")
age = input("How old are you? ")
print("Hello, " + name + "! You are " + age + " years old.")
In this example:
Important Note: The input() function always returns a string. If you need the input to be a different type, you’ll need to convert it.
age = int(input("How old are you? ")) # Convert input to integer
years_left = 65 - age # Calculate years left to retirement
print("You have", years_left, "years left until retirement.")
Here, we convert the user's input to an integer using int(). We then calculate how many years they have left until retirement (assuming retirement at age 65).
5. A Complete Interactive Program
Let’s combine what we’ve learned so far into one interactive program. This program will ask the user for their name and age, and then calculate how many years they have left until they turn 100.
# Get user input
name = input("Enter your name: ")
age = int(input("Enter your age: "))
# Calculate the year they will turn 100
years_to_100 = 100 - age
current_year = 2024
year_turn_100 = current_year + years_to_100
# Output the result
print(f"Hello, {name}! You will turn 100 years old in the year {year_turn_100}.")
In this program:
Sample Output:
Enter your name: John
Enter your age: 25
Hello, John! You will turn 100 years old in the year 2099.
Conclusion
In this article, we discussed:
This is just the beginning! With a strong foundation in variables and data types, you’ll soon be able to build more complex programs. In the next article, we’ll dive into Python operators and how they allow you to manipulate data in various ways.
Note:
This article was generated with the assistance of ChatGPT, an AI language model developed by OpenAI.
Results-Driven Strategic Leader | Project/Program Management | Six Sigma | Quality & Efficiency Champion | Operational Excellence
6 个月Very informative...