Journey of a Python Beginner · Lesson 1 (Part 1) | Python Basics: Expressions, Variables, and the First Program

🐍 Journey of a Python Beginner · Lesson 1 (Part 1) | Python Basics: Expressions, Variables, and the First Program

Hello everyone, I am Xingyuan, a 19-year-old programming novice self-learning Python 🤓.

This is my Learning Notes Series, where I will organize and share the knowledge points I learn every day with fellow learners on the same journey, hoping to progress together 🚀. 📌 Today’s learning content

👉 “Today we will learn the most basic components of Python: data types, variables, input and output, and write the first complete program!”

✨ Knowledge Points Explanation

1️⃣ Interactive Environment & Expressions

Concept

  • The <span>>>></span> that appears after opening IDLE is the interactive environment, executing one line at a time immediately.

  • Expression = value + operator, ultimately resulting in a result.

Code Example

>>>2+2      # Addition expression
4
>>>5-3
2
>>>2**3     # Exponentiation
8

Tip 💡

  • Press Ctrl-C to interrupt a frozen interactive environment.

2️⃣ Data Types

  • Concept

    • Integer (int): Whole numbers without decimal points.

    • Float (float): Numbers with decimal points.

    • String (str): Text surrounded by quotes.

  • Tip 🧐: Strings must be enclosed in matching single or double quotes, otherwise Python will be confused.

Data Type Example Values Description
int -2, 0, 42 Integer
float -1.25, 3.14, 0.0 Float
str ‘hello’, “42” String (text)

Code Example

>>>type(42)     # <class 'int'>
>>>type(3.14)   # <class 'float'>
>>>type("42")   # <class 'str'>

3️⃣ String Concatenation & Repetition

Concept

  • <span>+</span> represents “concatenation” between strings

  • <span>*</span> represents “repetition” between a string and an integer

Code Example

>>>'Hello'+'World'
'HelloWorld'
>>>'Hi'*3
'HiHiHi'
  • Concatenation: Use <span>+</span> to glue two strings together.

    >>>'Alice'+'Bob'
    'AliceBob'
  • Repetition: Use <span>*</span> to repeat a string N times.

    >>>'Alice'*5
    'AliceAliceAliceAliceAlice'

Common Mistake ⚠️

>>>'Alice'+42        # ❌ Error
TypeError: can only concatenate str (not "int") to str

4️⃣ Variables and Assignment

Concept

  • Variables are like labeled boxes, using <span>=</span> to put values inside.

  • Variable naming rules:

  1. Must be one word

  2. Can only contain letters/numbers/underscores

  3. Cannot start with a number

Example

spam=40          # Put 40 into spam
spam=spam+2    # Now spam is 42

Valid vs Invalid Variable Names

Valid Invalid Reason
balance current-balance Contains a hyphen
_spam 42account Starts with a number
account4 total$um Contains special characters

5️⃣ The First Complete Program hello.py

Source Code Line-by-Line Analysis

# This program says hello and asks for my name.
print('Hello world!')                    # 1️⃣ Print
print('What is your name?')              # 2️⃣ Prompt
myName=input()                         # 3️⃣ Read from keyboard
print('It is good to meet you, '+myName)
print('The length of your name is:')
print(len(myName))                       # 4️⃣ len() to get length
print('What is your age?')
myAge=input()
print('You will be '+str(int(myAge)+1) +' in a year.')

Example Output

Hello world!
What is your name?
Xingyuan
It is good to meet you, Xingyuan
The length of your name is:
2
What is your age?
19
You will be 20 in a year.

6️⃣ Common Built-in Functions Quick Reference

Function Example Usage Return Value Type
<span>print(x)</span> Displays x on the screen None
<span>input()</span> Reads a line of input from the user str
<span>len(s)</span> Length of string s int
<span>str(x)</span> Converts x to a string str
<span>int(x)</span> Converts string x to an integer int
<span>float(x)</span> Converts string x to a float float

✅ Summary

  1. Opened IDLE and typed <span>2 + 2</span> to see the result immediately.

  2. Learned about three basic data types: int, float, str.

  3. Learned to use <span>+</span> to concatenate strings and <span>*</span> to repeat strings.

  4. Variable assignment <span>=</span> and comparison <span>==</span> should not be confused.

  5. Always Ctrl-S to save the program before running it with F5!

  6. Wrote my first Python program that can ask for a name and greet!

📢 Interactive Question

👉 What was the first line of code you typed in the Python interactive environment? Share it in the comments!

Leave a Comment