Atom
Atom's Cove

Atom's Cove

Python 101: Syntax, Comments, Variables, Data Types, Numbers, and Type Casting

Photo by Glenn Carstens-Peters on Unsplash

Python 101: Syntax, Comments, Variables, Data Types, Numbers, and Type Casting

Atom's photo
Atom
·May 8, 2022·

7 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Introduction
  • Syntax
  • Variables
  • Data Types
  • Numbers
  • Type Casting
  • Conclusion

Introduction

Hello everyone, thank you for reading today's Python lesson. Today we will be covering a wide variety of topics such as syntax, comments, variables, data types, numbers, and type casting.

Since this is officially the first lesson, there will be 2 homework assignments posted for you at the end of the article. It is completely optional, but I recommend doing it to become more familiar with the topic. So, from that, let's get into our lesson!

Syntax

We talked about what syntax is and indentation in the previous lesson. To review, we learned that indentation refers to the spaces at the beginning of a code line. In other programming languages, the indentation is for readability only, but indentation in Python is required. You may review the examples provided in the previous lesson.

Commenting

In all programming languages, there is a feature known as commenting. Comments are used to explain Python code, make code more readable, and more. Comments are not read while the Python interpreter executes the code. Comments are only there for the programmer, meaning that the computer does not read comments.

How to create a comment?

In Python, comments starts with #:

# This code prints "hello world"
print("hello world")

Comments can also be placed at the end of a line. This means that Python will ignore the rest of the line.

print("hello world") # This code prints "hello world"

A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code. For example, say you are testing something, and you need to quickly remove a line, but you don't want to delete what you wrote. In this case, you can quickly comment the line, meaning that the interpreter will ignore it.

# print("Hello, world!")
print("Hi, matey!")

Multiline Strings

Just like comments, multiline strings are also ignored by the interpreter. This means that you can clearly express your ideas in multiple lines without the interpreter yelling at you for not writing valid code.

"""
This is a comment
written
in more than just one line

This is called a multiline string
and it is completely ignored by the interpreter
"""

print("Hello, world!")

Variables

Just like in math, variables are containers for storing data. For example, in math, you can assign values to a variable. For example:

x = 5
print(x)

It should return:

5

Variables have to follow specific naming syntax:

  • A variable name must start with a letter or an underscore
  • A variable name cannot start with a number
  • A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _)
  • Variable names are case-sensitive
    • This means that abc is not the same as Abc
    • This means that Abc is not the same as ABC

Here is an example of legal and illegal variable names:

# Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

# Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"

This isn't everything about variables, we will dive deeper in the next lesson. We will learn about how to assign multiple values to a variable, output variables, and global variables.

Data Types

In programming, data types are a very important topic. Variables can store data of different types, and different types can do different things. Python has the following data types built in by default:

CategoryType Name
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNoneType

Getting Data Types

You can get the data type of any object using the type() function:

x = 10
print(type(x))

This code should return <class 'int'>, meaning it is an integer. For now, let's just learn about the basic data types, leaving out binary types.

  • Strings
    • In Python, strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double quote, or triple quote. In Python, there is no character data type, a character is a string of length one (otherwise a single letter). Strings are represented by the str class.
  • Lists
    • Lists are just like arrays in other languages, which are an ordered collection of data. It is very flexible, as the items in a list do not need to be the same type as each other. All values inside a list can be changed, fetched, added, removed, etc.
  • Tuples
    • Just like a list, a tuple is also an ordered collection of Python objects. The only difference being that tuples are immutable (meaning they cannot be changed). You can only fetch values from a tuple, you cannot modify any of it like you would a list.
  • Booleans
    • A boolean can only be two values. Either True or False. Booleans are denoted by the class bool. Note: Unlike other languages, boolean values inside Python must have a capital T in True or capital F in false. This means that true and false is invalid in Python and it will throw an error.

In the next lesson, we will learn more about data types such as dictionaries and dive deeper into lists and multidimensional lists.

Numbers

In Python, there are three numeric types:

  • int
  • float
  • complex

In today's lesson, we won't worry about the complex type, as it's not too important. Let's focus on integers and floats. An integer is a whole number which isn't attached with any decimal value. For example, 5 is an integer along with 3.

A float, otherwise known as a "floating point number" is a number, positive or negative, containing one or more decimals. For example, 3.5 is a float along with 1.58437178894. Any number with one or more decimal values are considered a float.

Important Note: For example, let's take the numbers 3 and 3.0. Mathematically, they're both the same number, but in a programming POV, they're two different types of numbers. 3 is an integer (int) and 3.0 is a float (float).

Type Casting

There may be times when you want to specify a type onto a variable. This can be done with Python casting. Python is an OOL (Object-oriented language), and as such it uses classes to define data types, including its primitive types. Casting in python can be done using the following functions:

  • int() - constructs an integer number from an integer literal, a float literal (by removing the decimals), or a string literal (only if the string represents a number).
  • float() - constructs a float number from an integer literal (will add a .0 at the end of the number), a float literal, or a string literal (only if the string represents a float or a number).
  • str() - constructs a string from a wide variety of data types, including strings, integer literals, and float literals.

Examples with integers:

x = int(5) # x will be 5
y = int(2.8) # y will be 2
z = int("3") # z will be 3

Examples with floats:

x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

Examples with strings:

x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'

Conclusion

Hello! Thank you for reading through this lesson. I appreciate your support and will continue to make quality free Python 101 course lessons for you all.

Homework

I have two homework assignments for you all to do. One is a Google Form and the other is a hands-on assignment. Click here to fill out the Google Form (it's all multiple choice and your answers will be automatically checked).

Hands-on Assignment

Prompt: Your task is to create a simple Python program which follows the following requirements:

  • Must contain valid syntax
  • Comments explaining your code
  • Variables containing boolean values, string values, integer values, and float values
  • Print out the integer and float variable as a string

That's it! You can post your code in the comments below, I will review it :)

Questions

If you have any questions, make sure to leave them in the comments below and I'll answer them as soon as possible!

 
Share this