Python Quick Guide - Step 1: Basic Syntax and Data Types (3) - str, bool, None
Series - Python Quick Guide
Contents
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.
Step 1: Basic Syntax and Data Types
1.3. Basic Data Types
1.3.3. Strings (str
type)
- 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.
- If you want to use the same quotation marks inside and outside, you need to “escape” the inner quotes by inserting a backslash (
String Operations
- Adding strings (
+
) concatenates them.
- Multiplying a string by a number (
*
) repeats the string that number of times.
Type Conversion
- 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, aValueError
will occur.
- The same applies to the
float
function.
📚 Exercise
Display a string “Good evening!” with 30 spaces between “Good” and “evening!”.
1.3.4. True
and False
(bool
type)
- In Python,
True
represents something that is correct, andFalse
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 andFalse
if it is false, as shown below.
Type Conversion
- The
bool
function converts the input value to abool
type.- For
int
,0
is converted toFalse
and all other values are converted toTrue
. - For
float
,0.0
is converted toFalse
and all other values are converted toTrue
. - For
str
, an empty string (""
) is converted toFalse
and all other values are converted toTrue
.
- For
1.3.5. None
(NoneType
type)
- 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”
- It is used in scenarios such as:
None
is the only value of theNoneType
type.- Applying the
bool
function toNone
returnsFalse
.
📚 Exercise
Choose the correct output for the following code:
- “TrueFalseFalse”
- “TrueTrueTrue”
- “FalseFalseFalse”
- “TrueFalseTrue”
1.3.6. Other Data Types
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
, andbool
functions.