Python Programming

Jyothigolla
6 min readSep 10, 2024

--

Hello all..! let’s get undersatnd python basics of Litrals,Variables,Comments and Keywords in an easy understandable way.

PYTHON

What is Python?

Python is a high-level programming language that’s known for being easy to read and write. It’s like having a conversation in plain English, but with instructions for a computer.

Why Use Python?

  1. Easy to Learn: Python has a simple syntax (rules for writing code) that makes it easier for beginners to understand.
  2. Versatile: Python can be used for many different types of tasks.

Where Can Python Be Used?

  1. Web Development: Python can build websites and web applications. Popular frameworks like Django and Flask make this easier.
  2. Data Analysis: Python is great for analyzing data and making sense of large datasets. Libraries like Pandas and NumPy help with this.
  3. Artificial Intelligence and Machine Learning: Python is widely used in AI and machine learning for creating smart algorithms. Libraries like TensorFlow and scikit-learn are commonly used.
  4. Automation: Python can automate repetitive tasks. For example, it can be used to write scripts that handle files, send emails, or scrape data from websites.
  5. Game Development: Python can be used to create games. Pygame is a popular library for this purpose.

**Literals in python

In Python programming, literals are fixed values that are used directly in the code. They represent constant values that are directly written into the code and are not computed or derived from other values.

There are several types of literals in Python, each representing different kinds of data:

  1. String Literals: Represent sequences of characters enclosed in quotes. They can be single-quoted ('hello'), double-quoted ("world"), or triple-quoted for multi-line strings ('''this is a multi-line string''' or """another multi-line string""").
  2. Numeric Literals: Represent numbers. They include:
  • Integer Literals: Whole numbers without a fractional part, such as 42 or -7.
  • Floating-Point Literals: Numbers with a decimal point, such as 3.14 or -0.001.
  • Complex Literals: Numbers with a real and imaginary part, such as 2 + 3j.
  1. Boolean Literals: Represent truth values. There are two boolean literals: True and False.
  2. Special Literals:
  • None: Represents the absence of a value or a null value. It is often used to signify that a variable has not been initialized or to indicate the end of a sequence.
# Define some literals
name = "sree" # String literal
age = 30 # Integer literal
height = 5.9 # Floating-point literal
is_student = False # Boolean literal
subjects = ["Math", "Science"] # List literal

# Print the literals
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height}")
print(f"Is Student: {is_student}")
print(f"Subjects: {subjects}")

In Python, variables are used to store values, like numbers or text, that you can use later in your code. Think of them as labeled containers that hold data for you to work with.

**Variables in python

1. What is a variable?

A variable is a name given to a data value. You can think of it like giving a label to a value so that you can refer to it later in your program.

Example:

age= 25
name = "Alice"

Here, age is a variable that holds the number 25, and name holds the text "Alice".

2. Variable Assignment

Assignment in Python happens when you use the = symbol to assign a value to a variable.

Example:

x = 5
y = "Hello" # y is assigned the value "Hello"

n the left of the =, you have the variable name, and on the right, you have the value being assigned to it.

3. Dynamic Typing

Python is dynamically typed, which means you don’t need to specify the type of a variable (like integer, float, or string) when you create it. The type of the variable is determined automatically based on the value assigned to it. Also, you can change the type of the variable by assigning it a new value.

Example:

x = 5        # x is an integer
x = "Python" # now x is a string

Here, x first holds an integer (5), but later it holds a string ("Python"). You didn’t have to declare a specific type beforehand.

4. Conventions for Naming Variables

There are some common rules and conventions for naming variables in Python:

  • Start with a letter or an underscore: Variable names cannot start with numbers.
  • Use letters, numbers, or underscores: Variable names can contain letters (A-Z, a-z), numbers (0–9), and underscores (_).
  • Be case-sensitive: Python distinguishes between uppercase and lowercase letters (age is different from Age).
  • Descriptive names: It’s good practice to use meaningful names that describe what the variable represents, like total_price or user_name.

Example

user_age = 30  # Descriptive and easy to understand
_pi = 3.14 # It's okay to start with an underscore

** Comments in Python

Comments in Python are used to explain and clarify your code for others (or for yourself later). They don’t affect how the code runs but help improve readability and maintainability.

Why are comments important?

  • Readability: Comments make the code easier to understand by explaining the purpose or logic behind certain parts of the code.
  • Maintainability: When someone else (or even you in the future) looks at the code, comments help them quickly grasp what the code is doing, making it easier to modify or fix.

Types of Comments in Python

1. Single-line comments

Single-line comments are used for brief explanations or notes on one line of code. In Python, you create a single-line comment by adding a # before the comment text. Everything after the # on that line is ignored by Python

# This is a single-line comment
x = 5 # This variable stores the value 5
  • The first comment explains that it’s a comment.
  • The second comment is inline, meaning it’s on the same line as the code and explains the purpose of the variable x.

2. Multi-line comments

If you need to write a longer explanation or note that spans multiple lines, you can use multi-line comments. There are two ways to write multi-line comments in Python.

a. Using multiple single-line comments

You can simply use # at the beginning of each line.

Example:

# This code does the following:
# 1. Assigns a value to the variable
# 2. Prints the value to the console
x = 10
print(x)

b. Using triple quotes (‘’’ or “””)

You can also use triple quotes (''' or """) to create block comments that span multiple lines. Technically, these are not comments but strings that are not assigned to any variable. However, they can still serve as multi-line comments in practice.

Example:

"""
This is a multi-line comment.
It can span across multiple lines.
Useful for explaining complex logic or sections of the code.
"""
x = 10
print(x)

**Keywords in pyhton

Keywords are special reserved words in Python that have specific meanings and purposes. You cannot use these words as variable names, function names, or identifiers because they are part of Python’s syntax.

Role of Keywords in Python:

  • Defining structure: Keywords help define the structure of Python programs. They are used to write statements, loops, conditions, functions, and more.
  • Control flow: Some keywords control the flow of the program, such as loops (for, while) and conditional statements (if, else).
  • Built-in behavior: Keywords like def (to define a function) and class (to define a class) are used to build programs' core structures.

List of Python’s Reserved Keywords:

Here is a list of Python’s keywords. These may vary slightly depending on the Python version, but most are consistent:

False      await      else       import     pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

--

--