100 Days of Python Code: Day 1

Python Basics: Printing, Commenting, String Manipulation, and Variables

Resources:

  1. 100 Days of Code - The Complete Python Pro Bootcamp for 2021 on Udemy

  2. Replit: This is an online IDE for basically writing and running codes.

  3. Excitement😉😂

Python Basics

1. Printing

This is a way to send a text or a specified message to the screen. In Python, we can do this with the print function, print(). Now, let's try to use this function.

print("Hello World!")

When you run your program, the function above displays this:

image.png This simply means the text Hello World! has been printed on the screen.

However, you can see from the code we wrote that we actually typed in print("Hello World!") and not just print(Hello World!). This is because Hello World! is a string.

Strings: They are sequences of characters. In Python, they are surrounded by either single quotation marks or double quotation marks. This means 'Hello World!' is the same as "Hello World!".

Now, we know strings must be used with quotation marks. This means when we use them with functions they must be between quotation marks.

2. String Manipulation

  • Creating a new line: Sometimes, you need to print multiple strings on different lines. You can always use:
print('String 1')
print('String 2')
print('String 3')

However, to write cleaner code, you can use:

print('String1\nString2\nString3')

Although both methods give the same output, we should always try to write cleaner code.

image.png

  • Concatenating This is combining different strings to make a simple string. For example, if we want to combine the strings "Welcome" and "Home", we can do:
print("Welcome" + "Home")

When we run this, we'll see that the output is:

image.png

This shows that we have to manually add the space between the strings as Python doesn't do it automatically. So, we can use:

print("Welcome" + " " + "Home")

When we run this, we'll see that the output is:

image.png

3. Input Function

This is a way to allow a user to enter data and just like the print function, it looks like input(). Now, if we want to ask the user to enter their name, we can use:

input("What is your name?")

If we run this, we can see:

image.png

Then the user can enter a name.

image.png

To make this neater, we can let the user type the name on the next line with:

input("What is your name?\n")

Then, the output is:

image.png

4. Variables

These are names given to objects for future reference. In Python, there is no command for declaring a variable. This means the moment we assign an object to a variable name, the variable exists. Now, let's try to assign a variable using our last code.

 name = input("What is your name?\n")

With this, we have assigned the user's input the variable, name. Like we said earlier, we can reference this variable later. Now, let's try to do that.

name = input("What is your name?\n")
print(name)

When we run this, the user's input will be saved as the variable and then printed. You will notice that we did not put the name in quotation marks. This is because only strings require this.

We have learnt the basics of Python, I hope this was helpful to you.

Zainab🧡