DEV Community

Cover image for 🎁Learn Python in 10 Days: Day 1
William
William

Posted on

4 1 1 1 1

🎁Learn Python in 10 Days: Day 1

🎉 From today, I'm going to write a series of 10 articles titled "Learn Python in 10 Days". If you follow along and read diligently, by Day 11, you'll be a proficient Python developer! 🚀

Day 1: Python Basics

1. Literals

Literals are fixed values in your code. Here are some common data types in Python:

Type Description Example
Number (int) Integer value 10, -10
Number (float) Floating-point number 3.14
Number (complex) Complex number 3+4j (ends with j)
Boolean (bool) Represents True/False values True (1), False (0)
String (str) Text "Hello Python!"
List Ordered, mutable collection [1, 2, 3]
Tuple Ordered, immutable collection (1, 2, 3)
Set Unordered, unique items {1, 2, 3}
Dictionary Key-Value pairs {"key": "value"}

2. Comments

Comments explain your code. They aren't executed but can help others understand your code.

  • Single-line comments start with #
# This prints hello
print("hello")  # There should be a space after the `#`
Enter fullscreen mode Exit fullscreen mode
  • Multi-line comments use triple quotes (""")
"""
This is a multi-line comment.
It describes the purpose of the code.
"""
Enter fullscreen mode Exit fullscreen mode

3. Variables

Variables store data during program execution.

# Define a variable
balance = 100
print("Balance:", balance)

# Spend $10
balance = balance - 10
print("Balance:", balance)
Enter fullscreen mode Exit fullscreen mode

4. Data Types

The primary data types you'll encounter:

Type Description Example
int Integer 10
float Floating-point 3.14
str String "Hello!"

Use type() to get the data type.

print(type(10))      # <class 'int'>
print(type(3.14))    # <class 'float'>
print(type("Hi"))    # <class 'str'>
Enter fullscreen mode Exit fullscreen mode

5. Type Conversion

Convert between data types as needed.

# Convert to string
num_str = str(10)  

# Convert string to integer
num_int = int("20")  

# Convert float to int
float_int = int(3.14)  # Loses precision
Enter fullscreen mode Exit fullscreen mode

6. Identifiers

Names for variables, functions, etc. Must follow these rules:

  1. Use letters (a-z, A-Z), digits (0-9), and underscores (_).
  2. Cannot start with a digit.
  3. Case-sensitive.
  4. Can't use Python keywords.
# Correct
i_am_variable = 1
I_Am_Class = "Python"

# Incorrect
1_variable = "Nope"
class = "Nope"  # 'class' is a keyword
Enter fullscreen mode Exit fullscreen mode

7. Operators

  • Arithmetic operators: +, -, *, /, //, %, **
Operator Description Explanation
+ Addition Adds two objects; a + b outputs the result
- Subtraction Negates a number or subtracts one number from another; a - b outputs the result
* Multiplication Multiplies two numbers or returns a string repeated a certain number of times; a * b outputs the result
/ Division Divides a by b
// Floor Division Returns the integer part of the quotient; 9 // 2 outputs 4
% Modulus Returns the remainder of the division; 9 % 2 outputs 1
** Exponentiation Raises a to the power of b; a ** b
print("1 + 1 =", 1 + 1)
print("5 % 2 =", 5 % 2)  # Modulus
Enter fullscreen mode Exit fullscreen mode
  • Assignment operators: =, +=, -=
Operator Description Example
= Assignment Operator Assigns the value on the right to the variable on the left, e.g., num = 2 * 3 results in num being 6
+= Add AND assignment c += a is equivalent to c = c + a
-= Subtract AND assignment c -= a is equivalent to c = c - a
*= Multiply AND assignment c *= a is equivalent to c = c * a
/= Divide AND assignment c /= a is equivalent to c = c / a
%= Modulus AND assignment c %= a is equivalent to c = c % a
**= Exponent AND assignment c **= a is equivalent to c = c ** a
//= Floor Divide AND assignment c //= a is equivalent to c = c // a
num = 5
num += 3  # num = num + 3
print(num)
Enter fullscreen mode Exit fullscreen mode

8. Strings

Definition:

  • Single quotes: 'text'
  • Double quotes: "text"
  • Triple quotes: '''text'''
name = "John Doe"
quote = 'He said, "Python is awesome!"'
Enter fullscreen mode Exit fullscreen mode

String Concatenation:

greeting = "Hello" + " " + "World"
print(greeting)
Enter fullscreen mode Exit fullscreen mode

String Formatting:

age = 25
message = f"My age is {age}"
print(message)
Enter fullscreen mode Exit fullscreen mode

9. Input

Use input() to get user input.

name = input("Enter your name: ")
print(f"Hello, {name}!")
Enter fullscreen mode Exit fullscreen mode

Stay tuned for more awesome Python tips and tricks in the upcoming days! 🎉

Hope this helps! If you have any more questions, feel free to ask! 😊

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay