Day 1 of the Ultimate Learning Challenge : Mastering the Basics of Python

Ashika ArjunAshika Arjun
7 min read

Hellooo, welcome to day 1 of mastering the basics of Python. Today we will learn about variables in Python


Python is one of the most popular programming languages in the world, loved by developers for its simplicity and readability. Whether you’re interested in web development, data analysis, artificial intelligence, or even game development, Python has you covered.


Why Python?

Before we dive into the code, let’s talk briefly about why Python is such a great language for beginners:

  • Easy to Learn: Python has a clean, easy-to-read syntax, making it beginner-friendly.

  • Versatile: Python can be used for web development, data science, machine learning, automation, and more.

  • Large Community: With a large community of developers, you'll always find resources, tutorials, and support.


Setting Up Python

To get started, you need Python installed on your computer. Here's how you can do that:

  1. Download Python: Go to the official Python website: python.org, and download the latest version of Python.

  2. Install Python: Follow the installation instructions for your operating system. Make sure to check the box that says "Add Python to PATH" during installation.

  3. Check Installation: Open your terminal or command prompt and type python --version (or python3 --version on some systems). If installed correctly, you should see the version number.

Once installed, you can start coding in Python using an IDE (like PyCharm or Visual Studio Code) or a simple text editor (like Sublime Text). Personally I use PyCharm and it’s beginner friendly


Working with Variables in Python

1. Printing to the Console

The first thing every programmer learns is how to display information. Now, remember that the whole reason why we're learning to program is to be able to tell the computer what it needs to do, and to follow our commands.

Let’s tell the computer to do something. And we are going to do that by writing our first line of code. And it is called ‘print function‘. In Python, the function print() does that for you.

print("Hello, World!") # Output: Hello, World!
what’s in the console
Once it’s done, it indicates that by writing, “Process finished with exit code 0”, which means everything was successful, everything was run without any issues. Now, just to break down this area a little bit more for you, if you look at the top line, which you'll always see, there's a lot of text, but it's actually very simple to understand. Essentially, it's telling you the location of the current file that is being run that has resulted in this output. The second line, or multiple lines in between, is the result of your command to the computer. And then finally at the very end, it tells you if the process finished with success or if there are any other issues.

You have the keyword, "print", followed by a set of parentheses, and then inside the parentheses, you tell it what you want it to print. And once you've inserted that, then when this line of code gets executed by the computer, it'll know to simply print or output the thing that you've placed in between the parentheses. But notice here that it's not just the word, "Hello world!", that I've put inside my parentheses, I've also added double quotes around the word, and the reason why I've done this is so that I can tell the computer that this bit here, in between the double quotes, is not code. And that is called “String“, String of characters.


2. String Manipulation

Let’s do few more things with strings, if we wanted to print things on individual lines, then we actually had to write them a few times, right? We had to write print on three lines if we wanted it to be printed one, two, three, like so.

print("Hello, World!")
print("Hello, World!")
print("Hello, World!")

Using String manipulation we can modify, analyze, or formate strings. we can do the same thing, but by using a single print() method,

To create a new line, backslash and the n character (\n) is used. Now if we write "Hello world!" again afterwards, and hit Run, you can see "Hello world!" printed on two separate lines, separated by this \n character, which gets replaced by a new line.

print("Hello, World!\nHello, World!\nHello, World!")

Now, one of the other things that we can do with strings is we can concatenate them. We combine different strings so that they will be added to the end of another string and it is called “String Concatenation” .

print(Hello + " " + World)  # Output: Hello World

3. Python Input Function

We've seen the print function and all the things that we can do to strings and use the print function. But what if we wanted to be able to enter some data. In order to do that, instead of using the print function, we're going to use a different function, and it's called the input function.

The input() function in Python is used to take input from the user through the console.

input("A prompt for the user")
  • The text inside the quotes is the prompt that gets displayed to the user.

  • Whatever the user types is returned as a string.

The input function looks pretty much identical to the print function, but instead of the word print, it's just got the word input and inside the parentheses.

Example : Taking a name as input

print("Hello, " + input("Enter your name: ") + "!")

Output :

Enter your name: Alice
Hello, Alice!

4. Python Variables

Look at this line of code print("Hello, " + input("Enter your name: ") + "!") when we run this, we are pretty familiar with what happens, right? It will ask us for our name and when we hit Enter, nothing happens, but behind the scenes, this name or this input has now been received by this function. But once that's done, it kind of just disappears, right? And there's no way for us to be able to refer to it in the future. Well, this is where variables come, if I give the result of this action, a name, well, then I'll be able to refer to it later on.

name = input("Enter your name: ")
print("Hello, " + name + "!")

A variable in Python is a name used to store data (like numbers, text, etc.) so you can use it later in your code.

How to Create a Variable

x = 10
name = "Alice"
  • x stores the number 10

  • name stores the string "Alice"

Variable Types (Automatically Detected)

Python automatically figures out the type of data you store:

x = 10           # int
y = 3.14         # float
name = "Alice"   # str
is_valid = True  # bool

You can check the type using type():

print(type(x))      
print(type(name))

Changing Variable Values

x = 5
print(x)  # 5

x = 20
print(x)  # 20

Variables can also change types:

x = "Hello"
x = 123

Assigning Multiple Variables at Once

a, b, c = 1, 2, 3
name, age = "Alice", 25

Or give them the same value:

x = y = z = 0

5. Variable Naming

When naming variables in Python, you should follow some rules and best practices to make your code readable, clear, and error-free.

Rules (Must Follow)

  1. Start with a letter or underscore (_)

    • valid - name, _value

    • invalid - 1name, @score

  2. Only letters, numbers, and underscores are allowed

    • valid - user_name1

    • invalid - user-name, user name

  3. Case sensitive

    • Name, name, and NAME are all different variables
  4. Can't use Python keywords

    • invalid - if = 5, class = "hello"

Common keywords you can't use:

    if, else, for, while, class, def, return, import, try, True, False, etc.

Best Practices (Should Follow)

Use descriptive names:

age = 25          # Good
a = 25            # Not clear

Use lowercase letters and underscores for multi-word names:

user_name = "Alice"     # good (snake_case)
userName = "Alice"      # okay (camelCase, used more in JavaScript)

Stick to one style in a project

  • Python prefers snake_case for variables and function names.

Bad Examples:

1user = "Tom"        # starts with number 
user-name = "Tom"    # hyphen not allowed 
def = 5              # 'def' is a keyword

Good Examples:

user = "Tom"
user1 = "Jane"
user_age = 30
total_price = 99.99

:> To be Continued…

Happy Learning !

Thanks for reading :)

0
Subscribe to my newsletter

Read articles from Ashika Arjun directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashika Arjun
Ashika Arjun