Basics
Python is a beginner-friendly programming language that is simple, easy to learn, and widely used. In this tutorial, we will cover the basics of Python step by step. Let’s dive into the key concepts!
Basic Syntax and Keywords
What is Syntax?
- Syntax refers to the set of rules that define how Python code should be written.
- Python's syntax is clean and straightforward, making it easy to read and write.
Key Syntax Rules
1. Indentation
- Python uses indentation (spaces) to define blocks of code.
- Example:
if True: print("This is indented.") # Correct
Avoid missing indentation:
if True: print("This will cause an error.") # Incorrect
2. Case Sensitivity:
Python is case-sensitive:
name = "Alice" print(Name) # Error: 'Name' is not the same as 'name'
3. Keywords
Python has reserved words called keywords that cannot be used as variable names. Examples include if, else, for, while, def, and import.
Example:
if True: print("Python keywords in action!")
Comments and Documentation
What are Comments?
- Comments are used to explain the code. They are ignored by Python during execution.
- Why use comments?
- To make the code easier to understand.
- To leave notes for future reference.
Types of Comments:
1. Single-line Comment:
- Starts with #.
- Example
# This is a single-line comment print("Hello, Python!") # This prints a message
2. Multi-line Comment:
- Use triple quotes (''' or """) for multi-line comments.
- Example
""" This is a multi-line comment. It can span multiple lines. """ print("Multi-line comments are useful!")
Documentation Strings (Docstrings):
- Docstrings are special comments used to describe functions, classes, or modules.
- Example
def greet(): """ This function prints a greeting message. """ print("Hello!")
Variables
What are Variables?
- Variables are like containers that store data values.
- In Python, you don't need to declare the data type explicitly.
name = "Alice" # String age = 25 # Integer height = 5.6 # Float is_student = True # Boolean
Rules for creating variable
- It can contain only one word.
- It should use only letters, numbers, and the underscore (_) character.
- It should not begin with a number.
Example
# bad my message = 'Welcome' # good message = 'Welcome' # bad $%@message = 'Welcome' # good my_message = 'Welcome' # good my_message_2 = 'Welcome' # this is incorrect 12_message = 'hello'
Input and Output
Input from the User
- Use the input() function to take input from the user.
- This function will convert the input from the user into a string.
- Example:
name = input("What is your name? ") print("Hello, " + name + "!")
Output Example:
What is your name? Alice
Hello, Alice!
Converting Input
By default, input() returns a string. Use int() or float() to convert it. You can transform from string to integer or float.
Example:
age = int(input("Enter your age: ")) print("Your age is", age)
Output to the Screen
- Use the print() function to display messages or variable values.
- The print() function will write the value of the argument given to it.
- It can accepts multiple arguments of any data type.
Example
current_year = 2024 temperature = 20.5 three_nums = [1,2,3] print("Welcome") # Welcome print("Current year is ", curren_year) #Current year is 2024 print("Temperature is ", temperature) #Temperature is 20.5 print(three_nums) #[1, 2, 3]
'end' keyword
The output of the print functions adds newline to the end of the output. You can change this by changing the argument value of end.
Example
message = ["welcome","to","python", "tutorials"] for str in message: print(message) # The output will be in a new line #welcome #to #python #tutorials for str in message: print(message, end="-") # The output will be in a single line #welcome-to-python-tutorials-
'sep' keyword
The sep keyword can be used to separate the objects in print function.
Example
print("Day 1", "January", 2024, sep=",") # Day 1,January,2024
len() Function
The len() function will return the length of the given object. The return value will be of type number. If the object is of type string, it will return the number of characters. If the object is of sequence type, it will return the number of items in it.
print(len("Welcome")) # 7 print(len(['Jan', 1, 2024])) # 3