Skip to content

Programming Basics in Python

Print statements are one of the most basic and essential parts of programming. They serve as a fundamental tool for developers to display information to the user, debug code, and understand how data is being processed. Whether you are writing a simple script or developing a complex application, print statements can help you see what’s happening at each step of your program. Let’s explore how to use print statements effectively with examples from popular platforms like TikTok and Roblox.

Importance of Print Statements

  1. Debugging: Print statements are invaluable for debugging. They allow you to track the flow of your program and see the values of variables at different points.
  2. User Communication: Print statements can be used to display messages and instructions to the user, making the program interactive and user-friendly.
  3. Data Verification: By printing out data at various stages of processing, you can verify that your program is working correctly and identify where things might be going wrong.

In TikTok, you might want to print out a message to the user. Here’s an example of a print statement in Python:

print("Welcome to TikTok! Share your first video today!")

This will display the message “Welcome to TikTok! Share your first video today!” on the screen.

In Roblox, print statements can be used to show messages in the console, which is useful for debugging. Here’s an example:

print("Welcome to Roblox! Start creating your game now!")

This will display the message “Welcome to Roblox! Start creating your game now!” in the Roblox console.

Exercises

  1. Exercise 1.1: Debugging
    • Write a Python program that stores your favorite color in a variable and then prints it to help you debug if the variable is correctly assigned.
  1. Exercise 1.2: User Communication
    • Write a Python program that prints a message to inform the user that their profile is being updated.
  1. Exercise 2.1: Welcome Message
    • Write a Python program that prints the following message: Welcome to TikTok! Share your first video today!
  1. Exercise 2.2: Personalized Greeting
    • Write a Python program that stores your name in a variable and then prints a personalized greeting message.
  1. Exercise 3.1: Roblox Welcome Message
    • Write a Python program that prints a welcome message for Roblox users.
  1. Exercise 3.2: Personalized Roblox Message
    • Write a Python program that stores a player’s username and the number of Robux they have, then prints a personalized message.

Variables

Variables are used to store information that can be referenced and manipulated in a program. They act as containers for data values. Variables can hold various types of data, such as numbers, strings (text), lists, and more. Understanding how to use variables is crucial because they enable you to write flexible and dynamic programs. Let’s delve into examples of variables from TikTok and Roblox to see how they can be applied in different contexts.

Understanding Variables

Variables allow you to store data in a way that you can easily reference and manipulate. Here are some key points about variables:

  1. Naming Variables: Variable names should be descriptive and follow a consistent naming convention. This makes your code more readable and easier to understand.
  2. Assigning Values: You assign a value to a variable using the equals sign (=). For example, username = "TikTokUser123".
  3. Using Variables: Once a variable has been assigned a value, you can use it in your program to perform various operations or display its value.

Variables in TikTok

In TikTok, you might have variables representing different aspects of a user’s profile:

shares = 100 # Number of shares a video has
likes = 2500 # Number of likes a video has
followers = 15000 # Number of followers a user has
list_of_followers = ["user1", "user2", "user3"] # List of followers
bookmarks = 75 # Number of bookmarks a video has
title = "My First TikTok Video" # Title of a video
date = "2024-05-27" # Date the video was uploaded

Variables in Roblox

In Roblox, variables might represent a player’s profile details:

username = "RobloxPlayer123" # Username of the player
followers = 500 # Number of followers a player has
list_of_games = ["Game1", "Game2", "Game3"] # List of games created by the player
amount_of_robux = 1000 # Amount of Robux the player has

Comments

Comments are used to explain code and make it more understandable. They provide context and explanations for why certain pieces of code are written the way they are. Comments are crucial for maintaining and collaborating on code, as they help other developers (and your future self) understand the purpose and functionality of the code. Importantly, comments are ignored by the computer when the program runs, meaning they do not affect the execution of the code. Let’s look at how to add comments to our variables from TikTok and Roblox to make the code clearer and more maintainable.

Importance of Comments

  1. Improves Readability: Comments help make your code more readable and understandable for others who may read your code in the future.
  2. Provides Context: Comments can explain the purpose of variables, functions, and other code segments, providing context that may not be immediately obvious.
  3. Facilitates Collaboration: In a team environment, comments are essential for collaboration. They help team members understand each other’s code and intentions.
  4. Aids Debugging: Comments can help you remember why certain decisions were made, which is useful when debugging or updating code.

Types of Comments

  1. Single-Line Comments: Used for brief explanations or notes. In Python, single-line comments start with a #.
  2. Multi-Line Comments: Used for longer explanations that span multiple lines. In Python, multi-line comments are enclosed in triple quotes (''' or """).

Comments in TikTok

shares = 100 # Number of shares a video has
likes = 2500 # Number of likes a video has
followers = 15000 # Number of followers a user has
list_of_followers = ["user1", "user2", "user3"] # List of followers
bookmarks = 75 # Number of bookmarks a video has
title = "My First TikTok Video" # Title of a video
date = "2024-05-27" # Date the video was uploaded

Comments in Roblox

username = "RobloxPlayer123" # Username of the player
followers = 500 # Number of followers a player has
list_of_games = {"Game1", "Game2", "Game3"} # List of games created by the player
amount_of_robux = 1000 # Amount of Robux the player has

Write a Python program that stores the number of likes and shares for a TikTok video, then adds comments to explain each variable.

Input

The input function allows users to enter information into a program. This makes the program interactive, as it can ask for user input and then use that information to perform various tasks or calculations. Input functions are crucial for creating dynamic applications where user data is required. Let’s look at examples of how to use input when creating accounts on TikTok and Roblox.

Understanding the input Function

The input function is used to prompt the user for input and then capture that input as a string. Here’s the basic syntax in Python:

variable_name = input("Prompt message")

The prompt message is displayed to the user, and the user’s input is stored in the variable.

Input in TikTok

When creating a TikTok account, you might need to enter information such as your username and birthday:

username = input("Enter your TikTok username: ")
birthday = input("Enter your birthday (YYYY-MM-DD): ")
print("Welcome, " + username + "! Your birthday is set to " + birthday + ".")

Input in Roblox

When creating a Roblox account, you might need to enter your username and password:

username = input("Enter your Roblox username: ")
password = input("Create a password: ")
email = input("Enter your email (optional): ")
print("Welcome, " + username + "! Your account has been created.")

Write a Python program that prompts the user to enter their name and age, then prints a greeting message using the input.

Write a Python program that prompts the user to enter their TikTok username and birthday, then prints a welcome message.

Write a Python program that prompts the user to enter their Roblox username, password, and email, then prints a welcome message.

Summary

In this chapter, we introduced the basics of programming, including print statements, variables, comments, and input. We used examples from TikTok and Roblox to make these concepts more relatable and practical. By understanding these foundational elements, you’re on your way to becoming a proficient programmer!