Skip to main content

Python For Beginners - Python Variables & Comments

Python Variables

In Python, variables are used to store data values. A variable can hold different types of data, such as numbers, strings, or booleans. Here's an example of how to create and use variables in Python:

# Integer variable

num1 = 5

num2 = 10

sum = num1 + num2

print(sum)  # Output: 15


# String variable

name = "John"

print("My name is " + name)  # Output: My name is John


# Boolean variable

is_sunny = True

if is_sunny:

    print("It's a sunny day!")  # Output: It's a sunny day!

else:

    print("It's a cloudy day.")


In the above example, we first create two integer variables num1 and num2, then we create a variable called sum which holds the sum of num1 and num2. We then print the value of sum using the print() function.

Next, we create a string variable name and concatenate it with a string literal using the + operator.

Finally, we create a boolean variable is sunny and use it in an if-else statement to print whether it's a sunny or cloudy day.

Python Comments:


In Python, comments are used to add explanatory or descriptive notes within the code. Comments are ignored by the Python interpreter and are not executed as part of the program. They are solely intended for human readers to understand the code better.

There are two ways to write comments in Python:

Single-line comments: These comments are used for a single line of code and start with the # symbol. Anything after the # symbol on that line is considered a comment.

# This is a single-line comment

print("Hello, World!")  # This is another comment


Multi-line comments: These comments can span across multiple lines and are enclosed within triple quotes (`' ' '` or `" " "`).

'''
This is a multi-line comment.
It can span across multiple lines.
'''
print("Hello, World!")


It is common practice to use comments to explain the purpose of the code, provide information about the logic, document function or class definitions, or add any other relevant details to make the code more understandable and maintainable.

Here's an example that demonstrates the usage of comments in Python:

# This program calculates the sum of two numbers

# Define the numbers
num1 = 5 # First number
num2 = 10 # Second number

# Calculate the sum sum = num1 + num2 # Print the result
print("The sum is:", sum)


In the above example, comments are used to explain the purpose of the program, define variables, and clarify the steps involved in calculating the sum.



Comments

Popular posts from this blog

Python for Beginners - Python Data Types

Python supports several built-in data types, including: Numeric Types: Integer (int): Whole numbers without a fractional component. Floating-point (float): Numbers with a fractional component. Complex (complex): Numbers with a real and imaginary part. Sequence Types: String (str): A sequence of characters. List (list): An ordered collection of items. Tuple (tuple): An ordered, immutable collection of items. Mapping Type: Dictionary (dict): A collection of key-value pairs. Set Types: Set (set): An unordered collection of unique elements. Frozen set (frozenset): An immutable version of a set. Boolean Type: Boolean (bool): Represents either True or False. Binary Types: Bytes (bytes): A sequence of bytes. Bytearray (bytearray): A mutable sequence of bytes. These are the main built-in data types in Python. Additionally, Python also allows you to define and work with custom data types using classes and objects. Here are some examples of each data type: Numeric Types: # Integer x = 10 # Float...

Python for Beginners - Introduction to Python Programming

What is python? Python is an interpreted, interactive high-level programming language. It was designed to be effective at handling general-purpose programming demands and it has built a strong community of developers.  Python is often used as a scripting language for system administration and for rapid application development in web development, desktop applications, and elsewhere. It was developed by Guido van Rossum, and released in 1991. Python source code is also available under the GNU General Public License (GPL).  Python supports modules and packages, which encourages program modularity and code reuse.  Today, Python is very high in demand and all the major companies are looking for great Python Programmers to develop websites. Python is easy to get started with, so you won’t need to learn everything at once. You can start learning right away by using Python on the server or in a coding environment, or set up Python libraries that can read and modify files, connect...