My journey through learning Python: Day 1 to 3
Welcome back! I'm happy to see you here. Please, take a seat and enjoy the trip. π
Day One: Working with Variables in Python to Manage Data
This first day was an exciting starting point in the world of Python. Especially because @Angela Yu shares her way of programming and learning. She also helps us to organize our time. I found this very helpful because I work full time and spend 2 hours a day on public transportation. In total, I spend over 12 hours a day away from home. So coding for at least an hour a day on top of that, yeah, that scared me a little bit...
Let's get back to the point: variables in Python. They are the basic building blocks in Python programming that store values that can be used throughout the program. They are essential in data manipulation and analysis, and understanding how to work with them is fundamental in mastering the language.
But that was just the basics on this notion. For the interesting world of data types, we need to move to day two!
Day Two: Understanding Data Types and How to Manipulate Strings
I know that data types are one of the essential concepts that learners must understand when working with Python.
Afterall, like I said before, variables are the building blocks of programming languages, bacause they help categorizing and manipulating data effectively.
In this section, I will explore the different data types in Python and how to work with them.
The four main data types in Python are :
- Integers: they are whole numbers without any decimal point. They can be positive or negative. For example,
2
,10
,-5
,0
are all integers. - Floats: they are numbers with decimal points. They can also be positive or negative. ie.
3.5
,0.25
,-1.75
,0.0
are all float numbers. - Strings: they are a sequence of any characters enclosed in quotation marks (
' '
single quotes or" "
double quotes). The characters can be anything, like letters, numbers, symbols or spaces. For example"Hello, World!"
,'123_abc'
are both strings. - Booleans: they are just a data type that has only two values:
True
orFalse
. We don't know yet, but booleans are very helpful in programming languages, as they are the source of logical comparisons and decisions. For example, a comparison between two integers will either returnTrue
orFalse
, depending on wether the statement is true or false.
In Python, each data type has its unique set of methods and attributes that can be used to manipulate and work with data. For example, strings have methods that can be used to convert the case of letters, concatenate two strings, or split a string into separate words. Integers and floats, on the other hand, have methods that can be used to perform mathematical operations such as addition, subtraction, multiplication, and division.
Another essential aspect of data types in Python is type conversion. Type conversion is the process of converting one data type to another data type. This is important when working with different data types, such as converting an integer to a string or a string to a float. Python has built-in functions that can be used for type conversion, such as int()
, float()
, bool()
and str()
.
Day Three: Control Flow and Logical Operators
Speaking of booleans being the source of logical comparisons and decisions, here we are, right in the heart of the subject. Exciting, right? π€©
Control Flow & Conditional Statements
First, what does Control Flow means? Well, control flow refers to the order in which statements are executed in a program. In this day, we are learning about conditional statements.
if
Conditional statements are used to make decisions in a program. The most common conditional statement in Python is the if
statement. The if
statement evaluates a condition and executes the code inside the block if the condition is true. Here is an example:
x = 10
if x > 5:
print("x is greater than 5")
In this example, the if
statement checks if the value of x
is greater than 5. Since x
is equal to 10, the condition is true, and the code inside the block is executed, printing the string "x is greater than 5"
.
If the condition is false, the code inside the block is skipped.
else
You can also use the else
statement to execute code when the condition is false. Here is an example:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
In this example, the if
statement checks if the value of x
is greater than 5. Since x
is less than 5, the condition is false, and the code inside the else block is executed, printing the string "x is less than or equal to 5"
.
elif
In addition to the if
and else
statements, Python also has the elif
statement, which is short for "else if". The elif
statement allows you to check multiple conditions and execute different blocks of code based on each condition. The syntax for the elif
statement is similar to the if
statement, but it is used after the initial if
statement and before the else
statement. Here is an example:
x = 5
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is less than or equal to 5")
In this example, the if
statement checks if the value of x
is greater than 10. If it is, the code inside the first block is executed. If the condition is false, the elif
statement checks if the value of x
is greater than 5. If it is, the code inside the second block is executed. If the second condition is also false, the else
statement is executed, and the code inside the third block is executed. The elif
statement allows you to add multiple conditions to your program and handle each one appropriately.
Logical Operators in Python
Logical operators are used to combine conditional statements. Python has three logical operators: and
, or
, and not
.
and
The and
operator returns True
if both conditions are true. For example:
x = 5
y = 10
if x > 0 and y > 0:
print("Both x and y are positive")
In this example, the and
operator combines two conditions and returns True
if both conditions are true. The code inside the if statement is executed only if both x
and y
are positive.
or
The or
operator returns True
if at least one of the conditions is true. For example:
x = 5
y = -10
if x > 0 or y > 0:
print("At least one of x and y is positive")
In this example, the or
operator combines two conditions and returns True
if at least one of them is true. The code inside the if statement is executed if either x
or y
is positive.
not
The not
operator reverses the logical state of the condition. For example:
x = 5
if not x == 10:
print("x is not equal to 10")
In this example, the not
operator negates the condition x == 10
and returns True
if x
is not equal to 10. The code inside the if statement is executed only if x
is not equal to 10.
Recap' Tables
Here is some tables to make it more easy to comprehend, they are called Truth tables :
The and
Truth table
and | True | False |
True | True | False |
False | False | False |
The or
Truth table
or | True | False |
True | True | True |
False | True | False |
The not
Truth table
True | False | |
not | False | True |
Conclusion
Here is the end of this second article of my serie about my journey through learning Python. I hope you enjoyed it.
See you soon! π