Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Python 3 tutorial series outline 3 jpg

Data types and variables

[INTRO]

Hello and welcome to this tutorial on Introduction to Data Types and Variables in Python 3. In this tutorial, we will take a closer look at the different data types that are available in Python 3 and how to create and manipulate variables.

[DATA TYPES]

In Python 3, there are several built-in data types that you can use, including integers, floating-point numbers, strings, and lists.

INTEGERS:

  • Python 3 supports several built-in data types, including integers, floats, strings, and booleans.
  • Integers are whole numbers, and can be positive or negative. To create an integer in Python 3, simply assign a value to a variable, like this:
x = 1
y = -5
z = 100

FLOATING-POINT NUMBERS:

Floats are numbers with decimal points, and can also be positive or negative, such as 1.0, 2.5, and so on. To create a floating-point number in Python 3, simply assign a value to a variable, like this:

y = 10.5
pi = 3.14
neg_float = -2.5
small_num = 0.01

STRINGS:

Strings are sequences of characters and can be enclosed in single or double quotes. For example: “Hello, World!”. To create a string in Python 3, simply enclose a sequence of characters in quotes, like this:

name = "John Doe"
greeting = "Hello, World!"
language = 'Python 3'

LISTS:

Lists are ordered collections of items, such as [1, 2, 3]. To create a list in Python 3, simply enclose a set of items in square brackets, like this:

numbers = [1, 2, 3]

Booleans

Booleans are used to represent true or false values. They can be written as True or False.

is_raining = True
is_weekend = False

[Data Type Conversion]

  • In Python 3, you can convert one data type to another.
  • For example, you can convert a string to an integer using the int() function or convert an integer to a string using the str() function.
  • You can also use the float() function to convert a value to a float.
  • or convert a boolean to a float
# convert a string to a float
string_value = "3.14"
float_value = float(string_value)
print(float_value, type(float_value))

# convert an integer to a float
int_value = 5
float_value = float(int_value)
print(float_value, type(float_value))

# convert a boolean to a float
bool_value = True
float_value = float(bool_value)
print(float_value, type(float_value))

The output of this code would be:

3.14 <class 'float'>
5.0 <class 'float'>
1.0 <class 'float'>

In this example, we first convert a string to a float using the float() function, then convert an integer to a float, and finally convert a boolean to a float. The function returns the float representation of the input value and we can verify the type of the returned value using the type() function.

[VARIABLES]

  • In Python 3, a variable is a storage location for a value, and it has a name and a value.
  • To create a variable, you simply need to assign a value to a name using the assignment operator (=).
  • For example, to create a variable called “x” and assign the value 5 to it, and and “y” to assign the value 10, you would write:
x = 5 
y = 10
  • Once a variable is created, you can access its value by calling the variable name.
  • For example, if you want to print the value of the variable “x”, you would write:
print(x)  # Output: 5

[MANIPULATING VARIABLES]

Once you have created a variable, you can manipulate its value by using various operators, such as +, -, *, /, and so on. For example, to add two numbers together, you can do the following:

sum = x + y

Variable naming conventions and best practices in Python 3.

We already touched on the basics of naming variables in Python, now let’s look at what makes a good variable name.

Variable Naming Conventions:

In Python, variable names can consist of letters, numbers, and underscores. They must start with a letter or an underscore. It’s important to choose descriptive names for your variables, to make your code more readable.

Best Practices:

  • Use descriptive names: Instead of using single-letter variable names like “x” or “y”, use descriptive names that reflect the purpose of the variable. For example, instead of using “s”, use “student_name”.
  • Avoid using reserved words: There are a number of reserved words in Python that you cannot use as variable names, such as “if”, “else”, “for”, and “while”.
  • Use snake_case for variable names: In Python, it’s a common practice to use snake_case for variable names, where each word is separated by an underscore. For example, “student_name”.
  • Keep variable names short but descriptive: You want to choose names that are short enough to be easily remembered, but descriptive enough to convey the purpose of the variable.
  • Use consistent naming conventions: It’s important to use a consistent naming convention throughout your code. This makes your code easier to read and maintain.

By following these naming conventions and best practices, you can write more readable and maintainable code in Python. Remember, the goal is to make your code as clear and easy to understand as possible.

[Final Note]:

  • You can also create a variable without assigning it a value, but in that case, the variable will be of the type None, and if you try to perform any operation on it, it will raise an exception.
  • Python is a dynamically typed language, which means that you don’t need to specify the type of the variable when creating it, the interpreter will infer the type based on the value you assigned to it.

[Conclusion]

That’s it for this tutorial on Introduction to Data Types and Variables in Python 3. We have covered the different data types that are available in Python 3, including integers, floating-point numbers, strings, and lists, as well as how to create and manipulate variables

In the next video, we’ll be diving deeper into control flow with conditional statements and loops.

Thank you for watching and see you in the next tutorial.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
getting-stated-with-python-3

Getting started with Python 3

Next Post
What is the Big O notation?

Evaluating the time complexity of an algorithm

Please Leave a comment on comment section, let me know what you think about this article.

Related Posts
Hey, if you have any questions and want to talk to one of our specialists chat up here:
Chat me up