We are going to understand python syntax today. Now, every language has its set of rules also called as constructs. When you are starting to learn something, it is very important to follow those rules.
Python Syntax and rules are very simple and easy to understand. It is like reading and writing simple English.
Variables:
We need to store values like the name of start-up or budget, and what can be a better place than the computer memory.
And since we need that value to change as per our will, they are called as variables.
By definition, a variable is the name of the memory location of a computer system.
In python, we do not have to tell the variable whether it is going to be a number or characters, it just knows that and adjusts the memory size accordingly.
Variable declaration and Initialization
Please note the memory is allocated to variables only when it is declared and initialized.
Now let's try to declare some variables:
# Creating the name of start up | |
# there is no declaration what will be value or type | |
companyName = "The New Verve" | |
initalBudget = 15000 | |
perDayWage = 12.34 |
Rules for variable names:
- Python is case sensitive that means name and Name are two different variables.
- A variable name can include alphabets, numbers, and But It can only start with an alphabet or underscore.
- Variable names should follow camel case nomenclature. Our suggestion is to keep it for 2-3 words not more than that.
For example - companyName, intitalBudget, workingHours
Camel Case Style for Variable Names
Python Keywords:
These are the reserved words that have pre-defined meaning in the system. They need to be used for their specific purpose only.
You will get familiarized with them once we start using them in our programs.
So, don’t get overwhelmed by looking at them.
-
and
-
exec
-
Not
-
as
-
finally
-
or
-
assert
-
for
-
pass
-
break
-
from
-
print
-
class
-
global
-
raise
-
continue
-
if
-
return
-
def
-
import
-
try
-
del
-
in
-
while
-
elif
-
is
-
with
-
else
-
lambda
-
yield
-
except
Lines and Indentation:
Python code is clean, and there is no place for semicolon (;). And neither python uses curly braces, but that also means that indentation is the most crucial factor.
Spaces and tabs play the role of creating a block of code and are used to create readable code. We will explore this in future tutorials.
For now, don’t put spaces at starting of any line or else you will get an error.
Comments:
Comments are the lines followed by ‘#’ sign which program do not execute. These are going to be one of your best friends while programming.
You may not remember why you have declared a variable, or a block of code, or what was the idea when you start to program.
That’s when comments come in handy. We encourage you to write comments while writing code.
Input from user:
So, till now we have, find a company name, start an initial budget and also gave per day wage. But, what about getting feedback from a friend?
For this, there is a built-in function called as “input.” Following is the syntax:
Code for input:
# Code for asking for feedback or input from a user | |
feedbackFriend = input("Do you like my idea? (yes/no) : ") |
This will bring the cursor, and it will wait for user input.
You can ask for any input, ask anything in double quotes (“”), and collect the input in a variable for future use. You are going to use this function a lot in IT company or your college projects. After all its all about the user.
Print output:
But wait, what if we want to know what is the feedback from the user or the value of a variable. For that purpose, we are going to use print function.
Its syntax is print(""). And anything inside double quotes will be print as it is.
# Let's start with a ritual of programming by printing "Hello World!" | |
print("Hello World") | |
# you can also print the values if you want by using print function | |
# print has the following syntax | |
# print("statement or information", variableName) | |
print("company Name is", companyName) | |
print("Initial Budget is", initalBudget) | |
print("Per day Wage is", perDayWage) | |
# Let's print the value of feedback | |
print("Friend's feedback is : ", feedbackFriend) |
Key Takeaways:
- Variables are declared to hold the values which we want to use in future. They have specific rules for naming them, and you should always adhere to them.
- Keywords are reserved words which are intended for certain
- Indentation is something that will make your code beautiful and will make a block of codes.
- Comments will make your life easier, so use them
- User input will make your programs interactive.
- You can print anything on screen using print function.