Contents

Python Quick Guide - Step 1: Basic Syntax and Data Types (3) - str, bool, None

Info
  • This course is designed to help you learn the basics of Python programming as quickly as possible through hands-on practice.
  • The “Style Guide” sections primarily cover guidelines from PEP8 for writing clean Python code.
  • You can run and see the results of each code example.
    Feel free to experiment with the code - reloading the page will reset the content.
  • Strings are of the str type.
  • Strings are enclosed in either double quotes (") or single quotes (').
Style Guide
  • You can use either single quotes or double quotes, but it’s important to maintain consistency throughout your program.
  • When a string contains quotation marks, using the other type of quotes on the outside makes it more readable.
    • If you want to use the same quotation marks inside and outside, you need to “escape” the inner quotes by inserting a backslash (\) before them, which can make the code harder to read.
  • Adding strings (+) concatenates them.
  • Multiplying a string by a number (*) repeats the string that number of times.
  • You can use the str function to convert numbers to strings.
  • You can use the int function to convert strings to integers.
  • You can use the float function to convert strings to floating-point numbers.
  • If you apply the int function to a string that cannot be converted to an integer, a ValueError will occur.
  • The same applies to the float function.
📚 Exercise

Display a string “Good evening!” with 30 spaces between “Good” and “evening!”.

  • In Python, True represents something that is correct, and False represents something that is incorrect.
  • These are called boolean values and are of the bool type.
  • Conditional expressions return True if the condition is true and False if it is false, as shown below.
  • The bool function converts the input value to a bool type.
    • For int, 0 is converted to False and all other values are converted to True.
    • For float, 0.0 is converted to False and all other values are converted to True.
    • For str, an empty string ("") is converted to False and all other values are converted to True.
  • In Python, None represents a state of “nothing” or “having no value.”
    • It is used in scenarios such as:
      • Indicating that a search result was “not found”
      • Representing an input value or setting that “has not been specified”
      • Showing that a process “has not yet been completed”
  • None is the only value of the NoneType type.
  • Applying the bool function to None returns False.
📚 Exercise

Choose the correct output for the following code:

  1. “TrueFalseFalse”
  2. “TrueTrueTrue”
  3. “FalseFalseFalse”
  4. “TrueFalseTrue”

Python has other data types available by default (called standard types or built-in types),
such as the complex type for handling complex numbers and the bytes type for handling binary data.
If you’re interested, you can read the official documentation on built-in types.

Summary So Far
  • Lines beginning with a hash (#) are called “comments” and do not affect the execution results.
  • To display data such as strings and numbers on the screen, use the print function.
  • Python has the following built-in data types available by default:
    • int type, float type (numbers)
    • str type (strings)
    • bool type (True, False)
    • NoneType type (None)
  • You can check the data type using the type function.
  • You can convert data types using the int, float, str, and bool functions.

Related Content