DEV Community

Cover image for python programming for biginers day_2
augustineowino357-design
augustineowino357-design

Posted on

python programming for biginers day_2

Variables and Data Types in Python

In the previous lesson, we introduced Python and explored its key features. Today, we will learn about Variables and Data Types, which are among the most important concepts in Python programming.

Variables are used to store data in a program. They act like containers that hold information which can be used and modified later in the code.

Python makes variable creation simple because you do not need to declare the data type before assigning a value.


What is a Variable?πŸ€·β€β™€οΈπŸ€·β€β™‚οΈ

A variable is a name used to store data in memory.

Example:

name = "Mike"
age = 21
Enter fullscreen mode Exit fullscreen mode

In the example above:

  • "name" stores text data
  • "age" stores numeric data

Rules for Naming Variables

When naming variables in Python, follow these rules:

  • Variable names must start with a letter or underscore "_"
  • They cannot start with a number
  • Variable names are case-sensitive
  • Spaces are not allowed
  • Special characters are not allowed except underscore "_"

Valid Variable Names

student_name = "Brian"
_age = 20
totalMarks = 450
Enter fullscreen mode Exit fullscreen mode

Invalid Variable Names

2name = "John"
student-name = "Brian"
my name = "Alex"
Enter fullscreen mode Exit fullscreen mode

Data Types in Python

Data Types define the type of data stored in a variable.

Python has several built-in data types.


1. String (str)

A String is used to store text.

Example

course = "Python Programming"
Enter fullscreen mode Exit fullscreen mode

Strings are written inside quotation marks.


2. Integer (int)

An Integer stores whole numbers.

*Example
*

students = 120
Enter fullscreen mode Exit fullscreen mode

3. Float (float)

A Float stores decimal numbers.

Example

temperature = 36.5


4. Boolean (bool)

A Boolean stores only two values:

  • True
  • False

*Example
*

is_logged_in = True`
Enter fullscreen mode Exit fullscreen mode

Booleans are commonly used in conditions and decision making.


Checking Data Types

Python provides the "type()" function to check the type of data stored in a variable.

Example

name = "Python"
age = 10

print(type(name))
print(type(age))

Enter fullscreen mode Exit fullscreen mode

Output

<class 'str'>
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Multiple Variable Assignment

Python allows assigning multiple variables in one line.

Example

x, y, z = 10, 20, 30

print(x)
print(y)
print(z)
Enter fullscreen mode Exit fullscreen mode

Type Conversion

Type Conversion means changing one data type into another.

Example

age = "21"

converted_age = int(age)

print(converted_age)
Enter fullscreen mode Exit fullscreen mode

In this example, a String is converted into an Integer.


Why Variables are Important

Variables help programmers:

  • Store data
  • Process information
  • Build dynamic applications
  • Perform calculations
  • Create reusable programs

Without variables, programming would not be possible.


Conclusion
Variables and Data Types form the foundation of Python programming. Understanding how data is stored and managed helps beginners write better and more organized programs.

As you continue learning Python, variables will be used in almost every program you create.

Practice creating variables using different data types and experiment with printing their outputs.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)