Master Programming Essentials with Python

In the previous blog, we learned how to create your first Python program. In this blog you will master programming essentials with Python. It will cover the concepts of variables and data types, as well how they are used in python.

What is a variable?

In programming, a variable generally refers to a storage location identified by a name that can hold data values. It serves as a placeholder for data that can change during the execution of a program or process.

Variables usually have types that indicate what kind of data they can hold or store. Such as, integers, floating numbers, dates, strings, boolean etc. This is called variable type.

Every variable has a scope (local or global) that determines where it can be accessed. Variables are usually declared and assigned an initial value, a process also known as initializing of a variable. Their values can change (be reassigned) as the program runs.

Note: The variable declarations can vary in syntax depending on the programming language used to declare them.

See below two sample variable declarations:

string Location = “Ohio”

Int Age = 25

The above example shows two varaible declarations, first variable name is “Location” of type string with an initial value of Ohio.

The second variable name is “Age” of type integer with an initial value of 25.

Notice the string varaible value is specified in quotes (” “), whereas interger value is specified without the quotes. This is standard in programming, strings are always specified in quotes and numbers without any quotes.

Variables in Python

In Python, variable types are dynamic i.e., variable types is not declared explicitly but the type is dymamically allocated based on the value assigned to the variable.

Below example is written in Python using command prompt. If Python is not installed on your machine, follow how to install and verify your python setup or download Python from https://www.python.org/.

To learn some basics on python, read How to write your first program in Python.

In this example, two variables: `x` and `name` are declared and printed. X is assigned a number which makes it of type integer and name is assigned a string which makes its type as string. The print command is used to display the ouput on the screen.

Declaring and printing variables.

What are Data Types?

In programming, a data type defines the kind of data that can be stored in a variable and the operations that can be performed on that data. Data types help the compiler or interpreter understand how to handle the data.

The primitive or basic data types provided by most programming languages are:

  • Integer: Whole numbers (e.g., 10, -20)
  • Float: Decimal numbers (e.g., 12.5, -10.4)
  • Boolean: True or False values (e.g., True, False)
  • Strings: sequence of characters (e.g., “Hello”, “OCA”)
  • Character: Single character (e.g., ‘a’, ‘b’)
  • User-defined. Programmers can create their own custom data types

Data Types in Python

Python provides all the basic data types explained above and also a list datatype which is widely used.

The List data type as the name explains, allows you to store an ordered collection of items. Lists can hold elements of different data types, including other lists,

The snippet of python code below shows variables and their data types.

# is used for comments. Anytime you need to put a comment in Python use # symbol.

The Print command/function is used to display the output on the screen. It is used in conjuction with the type function which shows the data type of the variable.

The output, for example ,<class ‘int’> shows that the variable age is of type integer.

The above example, shows four variable of different data types:

1. age : This varaible is of type integer as it contains a whole number.

2. height: This varaible is of type float as it contains decimal numbers.

3. name: This varaible is of type string as it contains sequence of characters.

4. numbers: This varaible is of type list as it contains list of numbers.

Each data type serves a different purpose, enabling you to store and manipulate various kinds of information in programs.

Mastering the essentials of programming with Python provided solid understanding of variables and data types. By grasping how to use variables and data types, you set the stage for more advanced programming techniques and projects. If you have any questions, please mention them in the comment section.
Happy coding!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.