Python Syntax and Variables

Great! you have successfully Setup Environment for working on Python. Now, Let’s dive into the topic of Python syntax and variables for your Python Learning. Python syntax refers to the rules and structure that govern the writing of Python code, while variables are used to store and manipulate data in a program. 

In today’s lesson, we will learn about python syntax and Indentation, use of input/output functions and variables.

Python Syntax and Indentation

Python’s syntax emphasizes clean and readable code. One of its unique features is the use of indentation to define code blocks. Indentation can be achieved using spaces or tab characters. Consistency in indentation is crucial for proper code execution. Let’s see an example:

for i in range(5):
    print("This line is indented")
print("This line is not indented")

In the example above, the indentation after the for statement indicates that the following line is part of the loop. Now Do a Little changing in code, press tab before line 3 (this line is not indented), and re-run the code! The output will be completely different. Why? We will discuss the Answer of Why and more about Indentation in the upcoming chapter on conditional statements.

Print Function

The print() function is a versatile tool used to display output in Python. It allows us to showcase messages, variables, and intermediate results. The syntax for the print() function is as follows:

print(object(s), sep=separator, end=end, file=file, flush=flush)
  • object(s): One or more objects to be displayed. They can be variables, literals, or expressions.
  • sep: Optional parameter to specify the separator between the objects. The default is a space.
  • end: Optional parameter to define what character(s) to print at the end. The default is a newline character.
  • file: Optional parameter to specify the file where the output will be printed. The default is the standard output.
  • flush: Optional parameter to flush the output stream.

The print() function is invaluable for checking the values of variables, displaying program outputs, and debugging code. Throughout this course, we will frequently use this function to observe how our code is functioning. 

Let’s use it to print a welcoming message for our Python course:

print("Welcome to the Python Course!")

The output will be:

Welcome to the Python Course!

Variables and Naming Conventions

Variables in Python are used to store and manipulate data. When declaring variables, we need to follow certain rules and naming conventions:

  • Variable names can contain letters, digits, and underscores. They cannot start with a digit.
    e.g FirstName, firstname_, _firstname, first_name1, they all are valid, but 1stname isn’t valid, as we can’t start variable name with a digit.
    (Rule is same as you use for Instagram Username , Except that . is allowed there but not here)
  • Variable names are case-sensitive, meaning age and Age are considered different variables.
  • Choose descriptive names that reflect the purpose of the variable.
  • Avoid using reserved words or keywords as variable names.

Let’s look at an example to understand how to declare and use variables:

firstname = "Nexus"
secondname = "Pie"
print("Full name:", firstname, secondname)

In this example, we declared two variables, firstname and secondname, and assigned them the respective values. By using the print() function, we displayed the complete name by concatenating the variables.

It’s output will be

Full name: Nexus Pie

Variables are essential because they allow us to store and manipulate data throughout our programs. They provide flexibility and enable us to perform various operations on the data.

Comments in Python

Comments in Python are non-executable lines used to provide additional information within the code. They are essential for code documentation and improving code comprehension for both yourself and others who may read your code.

Comments start with the # symbol, and they can be used to explain the purpose of code sections, add clarifications, or temporarily disable certain lines.

# This is a comment explaining the purpose of the following code
x = 5  # This comment explains the value assigned to the variable

By including meaningful comments, we make our code more understandable and maintainable.

Data Types and Input Function

Python supports various data types, such as integers, floats, strings, booleans, lists, dictionaries, and more. Each data type has unique characteristics and operations associated with it. We will explore these data types in-depth in a later lecture dedicated to data handling.

To take input from users,

Python provides the input() function. It prompts the user to enter data, which can then be stored in variables for further processing. Let’s see an example:

firstname = input("Enter your first name: ")
lastname = input("Enter your last name: ")
print("Full name:", firstname, lastname)

In this example, the input() function prompts the user to enter their first name and last name. The values are stored in the firstname and lastname variables, respectively. The print() function then displays the full name by concatenating the variables.

Additionally, let’s create an attractive line based on the user’s age:

age = input("Enter your age: ")
print("You are", age, "years old. Wow, that's amazing!")

In this case, the user is prompted to enter their age, which is stored in the age variable. The print() function then prints a customized message that includes the age.

In our upcoming lecture, we will delve into data types, type casting, and operators. These topics will provide a comprehensive understanding of working with different data types and performing operations on them.

Remember to practice the examples and exercises provided to reinforce your understanding of Python syntax and variables.

Happy coding!

2 thoughts on “Python Syntax and Variables”

Leave a Comment