Getting Started with Python: Basic Data Types, Strings, and Lists


Basic Data Types, Strings, and Lists

Python is a popular programming language because it is simple to learn and powerful for building many kinds of applications. When starting with Python, it’s essential to understand data types, strings, and lists. In this article, we’ll explore these topics in a beginner-friendly way.

1. Variables and Data Types

In Python, variables store different types of data. Here are a few common types:

  • String: Text, written between quotation marks (e.g., "Hello, World!").
  • Integer: Whole numbers without decimals (e.g., 25).
  • Boolean: True or False values (e.g., graduated = True).

Using variables, we can print information about a person, like this:

name = "Ahmet"
age = 25
graduation = True
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Graduation: {graduation}")

With this code, we see the output showing Ahmet’s name, age, and graduation status.

2. Working with Strings

Strings are texts, and Python has many functions to handle them. For example, we can convert strings to uppercase, lowercase, or capitalize the first letter.

message = "I am learning Python."
print(message.upper())    # "I AM LEARNING PYTHON."
print(message.lower()) # "i am learning python."
print(message.capitalize()) # "I am learning python."

We can also replace parts of a string with another word. For example:

message = message.replace("learning", "love")
print(message) # Output: "I am love Python."

This is a helpful way to modify sentences and personalize messages in code.

3. Using Lists to Store Multiple Values

Lists in Python help store multiple values in a single variable. Lists are defined by using square brackets, like this:

fruits = ["apple", "banana", "strawberry", "pear"]

Here, we have a list of fruits. Lists allow us to:

  • Access specific items by their position (index),
  • Add new items to the list,
  • Remove items from the list.

For example

print(fruits[0])   # Output: "apple"
fruits.append("cherry") # Adds "cherry" to the end of the list
fruits.remove("banana") # Removes "banana" from the list
print(fruits) # Output: ["apple", "strawberry", "pear", "cherry"]

Conclusion

Learning Python basics like data types, strings, and lists is a great way to start programming. These concepts help you store, modify, and display data in useful ways. With practice, you’ll feel comfortable with these essential parts of Python and be ready to explore more complex topics.


berkayhasip.com

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top