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:
Python
Copy
if True:
    print("This is indented.")  # Correct

Avoid missing indentation:

Python
Copy
if True:
print("This will cause an error.")  # Incorrect

2. Case Sensitivity:

Python is case-sensitive:

Python
Copy
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:

Python
Copy
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
Python
Copy
# 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
Python
Copy
"""
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
Python
Copy
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.
Java
Copy
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

Python
Copy
# 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:
Python
Copy
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:

Python
Copy
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

Python
Copy
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

Python
Copy
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

Python
Copy
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.

Python
Copy
print(len("Welcome"))
# 7

print(len(['Jan', 1, 2024]))
# 3