🐍 Python: Comments, Variables, Input & Data Types
Complete Guide with Practice Exercises & Solutions
input(), understand data types (string, integer, float), and practice with real exercises and solutions.
What Are Comments?
Adding comments to code allows you to explain what the code does or to provide extra information about the code. In Python, you start a comment with a hash (#) symbol. Any text on a line after a # symbol will be ignored by the interpreter.
Comments in Action
# This is an Adventure Game about The Digital Sweet Shop
# The program was written by me
#
#
#
# The next few lines give the introduction
print("Welcome to The Digital Sweet Shop")
print()
print("You have been invited to take part in a competition in the shop.")
print("You must find the chocolate room where you will be asked a question.")
print("If you get it right you will receive letters which are part of a password and a clue.")
What is a Variable?
A variable is a named location in the computer's memory that stores data of a particular type. Data can either be assigned to a variable in the program itself, or it can be assigned from input from the user.
name = "Ardeel"
age = 12
address = "Station Road"
colour = "Red"
Printing Variables
To print the contents of a variable, use the print() statement. To combine text and a variable, use a comma (not inside quotation marks).
# Print just the variable
print(name)
# Output: Ardeel
# Combine text and variable
print("Your name is", name)
# Output: Your name is Ardeel
- Variable: A named memory location used to store data of a given type during program execution; a variable can change value as the program runs.
The input() Function
The input() command allows users to enter information into the program and store it in a variable.
name = input("What is your name? ")
print("Hello", name)
| Command | Description |
|---|---|
input() | Used to take input from a user. If you wish to store this input, you need to assign it to a variable. |
Comparison: Scratch Blocks → Python Code
| Scratch Blocks | Python Code |
|---|---|
|
when 🟢 clicked ask What's your name? and wait set [name ▾] to (answer) say (join [Hello ] (name)) |
name = input("What's your name? ")
print("Hello", name)
|
Practice Task: Name and Number
Create a program that:
- Asks the user to 'Enter your name' and stores it in a variable called
name - Asks the user to 'Enter a number between 1 and 10' and stores it in a variable called
number - Uses
printto create output like this:
Enter your name Khalil
Enter a number between 1 and 10 6
Your name is Khalil and the number you entered was 6
name = input("Enter your name ")
number = input("Enter a number between 1 and 10 ")
print("Your name is", name, "and the number you entered was", number)
What Are Data Types?
Variables can hold different data types. To store data, you must decide on a name and a data type for the variable first.
| Data Type | Example | Description |
|---|---|---|
| String | 'Hello World', 'WE5694MC' | Any textual characters (letters, numbers, symbols) |
| Integer | 24, -10 | Any whole number (no decimal point) |
| Real (Float) | 6.98, -0.045 | Any number with a decimal point |
Variable Examples
| Variable Name | Data Type | Reason |
|---|---|---|
playerage | Integer | Stores a whole number (age in years) |
playername | String | Stores letters/text characters |
The variable names have been chosen as playerage and playername, but they could have been called anything we wanted.
Input Function & Data Types
To capture data from the user, you use the input() function. The input function captures user input as a string data type. This contains numbers, letters, and symbols.
playername = input("What is your name? ")
print("Welcome", playername)
Examples of string data type: 'Robot', '@password123', '**WWW777'
Essential Vocabulary
- Data type: The different ways in which data can be stored, e.g., integer, string, decimal number
- Integer: Whole number
- Real: Any number with a decimal point, such as 1.2 or 56.8
- Float: Another name for the data type real; short for 'floating point number'
- Input function: A function that Python uses to capture string data from users
- String: Data that is made up of letters, numbers, or any characters on the keyboard
Rewrite Scratch Programs in Python
Program 1 (Scratch): Ask for a word and display it back
word = input("Enter a word: ")
print("The word you entered was", word)
Program 2 (Scratch): Ask for age and location
age = input("How old are you? ")
print("You are", age, "years old")
live = input("Where do you live? ")
print("You live in", live)
Identify Data Types
Look at the Python code below and identify the data type for each variable:
name = "Maryam"
cardNumber = "0012563943029845"
balance = 145.98
address = "Mall Road"
age = 11
| Variable Description | Variable Name | Data Type | Explanation |
|---|---|---|---|
| Name on bank card | name | String | Contains text characters representing a name |
| Bank card number | cardNumber | String | Stored as text because it contains leading zeros and is not used for calculations |
| Bank account balance | balance | Float (Real) | Contains a decimal point representing monetary value |
| Address | address | String | Contains a mix of letters and numbers representing text |
| Age | age | Integer | Contains a whole number representing years |
Complete the DataTypes.py Program
Edit the code so when it runs, it looks like this:
Name: Maryam
Age: 13
Address: Station Road
Bank Card Number: 0012563943029845
Current Balance: 145.98
# Updated variables to match final output requirements
name = "Maryam"
cardNumber = "0012563943029845"
balance = 145.98
address = "Station Road"
age = 13
# Output formatting statements
print("Name:", name)
print("Age:", age)
print("Address:", address)
print("Bank Card Number:", cardNumber)
print("Current Balance:", balance)
❓ Frequently Asked Questions (FAQ)
Expert ICT, Computer Science, Coding & Digital Marketing Classes
Learn ICT, Computer Science, Programming, Web Development and Digital Marketing with practical, career-focused guidance.
💬 Join WhatsApp ▶ Watch Tutorials🚀 Learn Today. Build Tomorrow.
Welcome to our Sri Lanka Online ICT & Computer Classes community! We provide Online Tuition Classes, Home Visit Classes and Individual / Group Classes for students and professionals. Whether you are a school student, university student, beginner programmer or entrepreneur, we help you develop practical IT skills for education and career growth.
💡 Explore Our Training & Services
🎓 University Project GuidanceTechnical guidance for university assignments and final-year IT projects.
|
🤖 AI, Coding & SoftwareDevelop programming knowledge and practical software development skills.
|
🏫 School ICT ClassesInteractive ICT and Computer Science lessons for school students.
|
🌐 Web Design & DevelopmentBuild websites and database-driven applications for individuals and businesses.
|
📢 Digital MarketingGrow your brand online with social media and digital marketing support.
|
💻 Computer & IT SkillsBuild essential computer skills from absolute beginner to advanced level.
|
📚 Learn With Your Curriculum
Support for Sri Lankan Local Curriculum, Cambridge and Pearson Edexcel programs.
🇱🇰 Sri Lankan Local Curriculum
|
📘 Pearson Edexcel
|
📗 Cambridge Curriculum
|
💻 Programming & Software Development
🛠️ Programming Languages & Technologies
| ✅ Python Programming | ✅ C Programming | ✅ Java Programming |
| ✅ PHP Programming | ✅ JavaScript | ✅ HTML & CSS |
| ✅ jQuery & AJAX | ✅ ASP.NET | ✅ VB.NET |
| ✅ VB6 | ✅ MySQL | ✅ MS SQL Server |
| ✅ MS Access | ✅ Oracle Database | ✅ Database Design |
| ✅ SQL Queries | ✅ Database Programming | ✅ Software Development |
🌐 Web Design & Development
- ✅ Website Design
- ✅ Website Development
- ✅ WordPress Website Development
- ✅ HTML / CSS / JavaScript
- ✅ PHP & MySQL
- ✅ WordPress SEO
- ✅ Website Maintenance
- ✅ Web Applications
- ✅ Database-Driven Websites
🎓 University & Final Year IT Projects
🧑💻 Project Development
|
📄 Documentation & Practical
|
🧠 AI & Smart Applications
|
📊 Database, Cloud & IT Training
| ✅ Oracle Database | ✅ MySQL | ✅ Microsoft SQL Server |
| ✅ Microsoft Access | ✅ Database Design | ✅ SQL Queries |
| ✅ AWS Cloud | ✅ Linux | ✅ UNIX |
| ✅ Windows Server | ✅ DevOps | ✅ SRE |
| ✅ Automation | ✅ Software Testing | ✅ Freelancing Skills |
📱 Digital Marketing & Social Media
| ✅ Digital Marketing | ✅ Facebook Page Setup & Management |
| ✅ Instagram Marketing | ✅ TikTok Marketing |
| ✅ YouTube Channel Setup | ✅ Social Media Management |
| ✅ Content Creation | ✅ SEO |
| ✅ Facebook Ads | ✅ Instagram Reels |
| ✅ LinkedIn Branding | ✅ Social Media Campaign Planning |
| ✅ Hashtag Strategy | ✅ Insights & Engagement |
🖥️ Microsoft Office & Digital Skills
- ✅ MS Word
- ✅ MS Excel
- ✅ MS PowerPoint
- ✅ Excel Formulas
- ✅ Excel Automation & Macros
- ✅ Computer Fundamentals
- ✅ Digital Literacy
- ✅ Multimedia Presentations
⭐ Why Join Our Classes?
|
🌍
Online & Home Visit Classes
|
👥
Individual & Group Classes
|
🗣️
English / Sinhala / Tamil
|
📚
School & University Support
|
|
📝
Past Papers & Model Papers
|
💻
Practical Programming
|
🚀
Beginner to Advanced Skills
|
🎯
Career-Focused Learning
|
📞 Ready to Start Learning?
Book your online class or contact us for ICT training, programming courses, university project guidance and digital marketing services.
📱 Call / WhatsApp: +94 729622034
📧 Email: itclasssl@gmail.com
💻 Skype: ITClassSL
💬 Contact Us on WhatsApp🌐 Our Online Learning Platforms
Explore tutorials, notes, project guides and learning resources.
ITClassSL — ICT Training | Computer Classes | Programming | University Project Guidance | Digital Marketing
Learn • Practice • Build • Grow 🚀
No comments:
Post a Comment