Urgent.News

What's breaking now, across thousands of outlets.

Tech

Python Finally Started Making Sense When I Stopped Treating It Like Magic ๐Ÿ

Iโ€™ll be honest. When I first started learning Python, I looked at code and thought: โ€œOkay... but why does the computer understand this?โ€ ๐Ÿ˜‚ There were variables, strings, integers, floats, booleans, input() , type() , f-strings... It felt like learning a new language where everyone had forgotten to give me the dictionary.But after practicing the basics, something started to click. Python isn'tโ€ฆ

I remember my first encounter with Python. I stared at the code, bewildered. "How does a computer comprehend this?" I wondered. Variables, strings, integers, floats, booleans - it felt like learning an entirely new language. But as I practiced the fundamentals, Python began to make sense. Here are some revelations that transformed my understanding.

1. Variables are like small boxes. I could label them: name, age, city. Then I could assign values to them: name = "Amina Wanjiku", age = 24, city = "Nairobi". Python understood: "name equals Amina Wanjiku, age equals 24, city equals Nairobi". Asking Python to display the contents was simple: print(name), print(age), print(city). This made programming feel less mysterious - variables were just named storage for data.

2. Not all numbers are the same to computers. Consider 7 and "7". In Python, 7 is an integer, while "7" is text. When I added them: print(7 + 7) = 14, but print("7" + "7") = "77". Python doesn't care about the appearance of data; it cares about the data type. This simple distinction opened my eyes to the importance of data types in programming.

3. Python has four basic data types to start: strings (str), integers (int), floats (float), and booleans (bool). I met them one by one: name = "Nelly", age = 22, balance = 74670.50, is_student = True. When I got confused about a variable's type, Python stepped in to clarify: print(type(age)) would respond with "<class 'int'>". Python was my guide in understanding data types.

4. I learned how to make Python interact with me through the input() function. Before, my programs were strictly instructions from me to Python. But with input(), I could ask the user questions. For example:

name = input("What is your name? ")

print("Hello", name)

This allowed my program to have a conversation with the user, making it interactive and dynamic.

5. Applying my accounting background to Python was a thrilling experience. I created a simple money transfer program:

sender = input("Enter sender name:")

recipient = input("Enter recipient name:")

amount = int(input("Enter amount to send (Ksh):"))

charge = 11

total = amount + charge

This code calculated the total cost of a money transfer, integrating my financial knowledge with programming. It was exciting to see Python's potential in real-world applications.

6. Finally, I discovered f-strings. They made my print statements cleaner and more readable:

Instead of:

print("Name:", name, "Age:", age)

I could write:

print(f"Name: {name}, Age: {age}")

This concise syntax was a game-changer, streamlining my code and making it easier to understand.

Written by urgent.news from Dev.to's reporting โ€” not their text. Machine-written โ€” may contain errors; check the original before relying on it.

Read the original at dev.to โ†’

More in Tech

Finally Teaching Python to Think ๐Ÿ

When I started learning Python, I was mostly telling it what to print. Then I discovered if statements and loops. And suddenly, Python could make decisions, repeat tasks, and even help me analyseโ€ฆ

More from Sunday 20 September โ†’