This content originally appeared on DEV Community and was authored by William
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 `#`
-
Multi-line comments use triple quotes (
"""
)
"""
This is a multi-line comment.
It describes the purpose of the code.
"""
3. Variables
Variables store data during program execution.
# Define a variable
balance = 100
print("Balance:", balance)
# Spend $10
balance = balance - 10
print("Balance:", balance)
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'>
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
6. Identifiers
Names for variables, functions, etc. Must follow these rules:
- Use letters (a-z, A-Z), digits (0-9), and underscores (_).
- Cannot start with a digit.
- Case-sensitive.
- Can’t use Python keywords.
# Correct
i_am_variable = 1
I_Am_Class = "Python"
# Incorrect
1_variable = "Nope"
class = "Nope" # 'class' is a keyword
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
-
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)
8. Strings
Definition:
- Single quotes:
'text'
- Double quotes:
"text"
- Triple quotes:
'''text'''
name = "John Doe"
quote = 'He said, "Python is awesome!"'
String Concatenation:
greeting = "Hello" + " " + "World"
print(greeting)
String Formatting:
age = 25
message = f"My age is {age}"
print(message)
9. Input
Use input()
to get user input.
name = input("Enter your name: ")
print(f"Hello, {name}!")
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!
This content originally appeared on DEV Community and was authored by William