Urgent.News

What's breaking now, across thousands of outlets.

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 data. ๐Ÿ‘€ That was when things started getting interesting. ๐Ÿง  Teaching Python to Make Decisions I learned that comparison operators allow Python to ask questions: score = 75 print ( score >= 50 ) # Trueโ€ฆ

When the author began learning Python, they mostly focused on telling the code to print specific outcomes. However, they soon discovered the power of if statements and loops. With these tools, Python could begin making decisions, repeating tasks, and even analyzing data, which marked the beginning of an exciting journey.

To teach Python to make decisions, the author learned about comparison operators such as score = 75 and how to print True or False based on whether the score equals 50 or not. Using if, elif, and else statements, the author programmed Python to evaluate scores and print the corresponding grades.

The concept of nested if statements was introduced, allowing for checking login credentials. For example, if the username was "admin" and the password was "Kenya2025," Python would print "Welcome Admin." Otherwise, it would print "Wrong Password" or "User not found."

Loops, another key concept, enabled Python to repeat specific tasks. Instead of printing a greeting for each individual separately, the author could write a single line of code using a for loop to print greetings for Alice, Brian, and Jane.

The author's accounting background came in handy while working with M-Pesa transactions. By using a for loop, Python could calculate the total amount of these transactions. This experience demonstrated how Python could handle numerical data and perform calculations.

As the author progressed, they created a savings calculator using a while loop. By inputting their savings goal and monthly savings amount, Python would determine how many months it would take to reach that goal. In this case, with a Ksh 50,000 savings goal and Ksh 8,000 monthly savings, Python calculated that it would take 7 months.

Throughout this journey, the author has shifted their mindset from merely understanding the meaning of code to identifying real-world problems that Python can solve. They have learned about if, for, while, and while loops, as well as the use of accumulators and conditional statements. Despite still being a beginner, the author is excited about the process of turning real-world problems into instructions that a computer can understand, and they find it increasingly addictive to watch Python solve these problems.

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

The FCC vs the PRC

Brendan Carr, President Trumpโ€™s combative chair of the Federal Communications Commission, has made his agency a key player in U.S. China policy.

More from Sunday 20 September โ†’