DEV Community

Ron Njuguna
Ron Njuguna

Posted on

Python Basics: Getting Started with Variables, Data Types Input and Output

After learning the basics of SQL, I started working with Python. Python is one of the programming languages that is widely used in areas such as web development, automation, data analysis, artificial intelligence and scripting.

As a beginner, I found that understanding the basic syntax and how Python handles data is important before moving on to more advanced topics.

In this article, I will go through some of the Python basics I have learned, including variables, data types, input/output and basic Python syntax.
A simple python syntax can be written as:


The print() function displays information on the screen.

Variables
A variable is used to store a value that we can use later in a program.
For example:


Here, name stores the text "Ron" while age stores the number 22.
We can then use these variables:


The output would then be:


One thing I noticed when learning Python is that I don't have to specify the data type when creating a variable. Python determines the type based on the value assigned to it.
For example:


Python Data Types
Python has several built-in data types. Some of the common ones I have learned are:
str – used for text
int – used for whole numbers
float – used for decimal numbers
bool – represents True or False
list – stores multiple values in a collection
A good example which shows the use of the types above are:


We can use the type() function to check the data type of a variable:

 This is useful when working with data because it helps us understand what type of information a variable contains.
Input and output
Programs are more useful when they can interact with users. Python provides the input() function to receive information from a user.
An example is:

If the user enters:
Ron
The program will display:
 One important thing I learned is that input() returns the user's response as a string. If we want to work with a number, we need to convert it. For example:

 Here, int() converts the input into an integer.
Basic Python Syntax
Python syntax is relatively straightforward, but indentation is very important. For example: The indentation tells Python which statements belong to the if and else blocks. Unlike some programming languages, Python does not require curly brackets {} to define these blocks.
Python also allows us to add comments to our code: Comments are useful because they help explain what different parts of our code are doing.
A Practical Example
We can combine some of these concepts to create a simple program for calculating the total cost of a product. If I put the product name as Sugar, Quantity as 5 and Price as KSh2,000 the output would be: Conclusion
Learning Python starts with understanding the fundamentals. Variables allow us to store information, data types tell us what kind of information we are working with and input/output allows our programs to interact with users.
I have also learned that Python's syntax is designed to be readable, but things such as indentation, correct data types and variable names are still important when writing programs.
These concepts may seem simple, but they provide the foundation for more advanced Python topics such as loops, functions, object-oriented programming, file handling and working with databases.
For me, the most important lesson at this stage is not just memorizing Python syntax but understanding how these basic concepts can be combined to solve simple, real-world problems.

Top comments (0)