Python Programming Day2

Table of Contents:

1.Python Comments

2.Python Variables

3.Concatenation(Using + operator)

1)Python Comments:

Python comments are used to add explanatory or descriptive text within the code.

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Here are some key points about Python comments:

Single-line Comments: 

Single-line comments begin with the hash (#) symbol and continue until the end of the line. They are used to provide brief comments or annotations.

Example:

#This is single line comment
print("Hello Python")

Comments can be placed at the end of a line, and Python will ignore the rest of the line:

Example:

print("Hello Python") #This is single line comment


Multi-line Comments: 

Python does not have a specific syntax for multi-line comments like some other programming languages. However, you can use multi-line strings (enclosed in triple quotes) as a way to create multi-line comments.


'''This is multiline comment
written in
more than just one line'''

print("Hello Python")


(or)

"""This is multiline comment
written in
more than just one line """

print("Hello Python")

Commenting Best Practices: 
Consider the following best practices when using comments in your Python code
  • Use comments to explain complex code, algorithms, or logic.
  • Write clear and concise comments that provide meaningful insights.
  • Avoid redundant comments that merely restate the obvious code.
  • Maintain consistent formatting and indentation for comments.
  • Update comments when modifying the code to keep them accurate and relevant.
2)Python Variables:

Variables are containers for storing data values. A variable in python is a name given to a memory location in a program.

Rules for Python variables:
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name cannot be any of the Python keywords.
#valid variable Names
a=5
_b=10
a_b=30
m1=20
name="ram"
NAME="mani"
_a_b=25

 
#Invalid variable Names
1a=10
a b=20
$b=30
x-y=20

First create variable and after that assign a value to it.

a=10
print(a)

In above Example:
a is Variable
value 10 is assigned to variable a

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example:
a=10
a="Python"
print(a)

Output:
#output
Python

Explanation:
Variable a with type int
after that we assign string "Python" to same variable a and now type of variable a is String

Assign Multiple Values:
Python allows you to assign values to multiple variables in one line

Assigning Many values to Multiple Variables:

a,b,c=1,2,3
print(a)
print(b)
print(c)

#ouput
1
2
3

Assigning one value to Multiple Variables:
 
a,b,c=10
print(a)
print(b)
print(c)

#ouput
10
10
10

Output Variables:
The Python print() function is often used to output variables.
print("Hello Mani")

In the print() function, you output multiple variables, separated by a comma:

a="I am"
b=24
c="years old."
print(a,b,c)
#output
I am 24 years old.

3)Concatenation (Using + Operator):
 
a="I am "
b="24 "
c="years old."
print(a+b+c)

#ouput:
I am 24 years old.

We cannot concatenate(means add) string and integer

Example:
a="10"
b=5
print(a+b)

#ouput:

File "main.py", line 2, in <module>
    import user_code
  File "/tmp/user_code.py", line 3, in <module>
    print(a+b)
          ~^~
TypeError: can only concatenate str (not "int") to str

Post a Comment

Previous Post Next Post

Contact Form