Curriculum
By the end of this lesson, you should be able to:
type() and print() for variable inspection and output.Syntax refers to the set of rules that define the structure of a Python program. Just like grammar in spoken languages, Python has its own grammar that must be followed exactly, otherwise, your code would not run properly.
Key syntax rules in Python:
if age > 18:
print("You are an adult.")
Name and name are valid variables but are different from each other, and none can be used in place of the other.:) are used after keywords such as if, for, while, def, and class to signal the start of an indented block.As you write more Python programs, the application of the above rules will become more natural
A variable is like a container that holds information, such as a number, a name, or a result. You need variables to hold quantities that would be used in programming.
In most programming languages, a variable must be declared before use. If a string (a combination of characters) needs to be stored, a variable must be declared as a string type before it is used to store the string.
Python does not require the declaration of a variable type before its use. The interpreter will determine this based on the use and context of the variable. For instance, you can simply use the following variables in Python
name = "Ada"
age = 32
is_student = True
The Python interpreter automatically determines the type of the variable based on the value assigned to it.
For clarity, lets use the Java language as an example (don’t worry, you do not have to know Java for this).
Suppose you wanted to declare and assign a string in Java, say a string like “Ada”, to a variable called name, you would do the following.
String name = "Ada";
Observe from the above code that you would need to declare the variable as a “String” before its use as shown above.
Importantly, in Python, since the type is determined from the context, you can re-assign a variable at any time:
age = "George" # Although this is supposed to be an integer, the assignment is valid
Although you would not be changing the type of a variable in most cases, the re-assignment of a string to the age variable is valid.
The specification of variable names in Python follows a set of rules. As you create more variables, you should eventually get used to the rules and they will become second-nature. The rules are stated below:
_)Here are a few valid and invalid declarations:
Valid: user_name, _score, temp1
Invalid: 1score, user-name, class (reserved word)
Variable Recommended style: Python variables are often declared using the snake_case . This is a form of writing variable names where all the characters are in lower case and words are separated by underlines. Here is some information on the other options.
Python supports several built-in data types. The most common ones are listed in the table below:
| Type | Description | Example |
|---|---|---|
int |
Integer number | 5, -2, 0 |
float |
Decimal number | 3.14, -0.5 |
str |
Text (string) | "Hello", 'Python' |
bool |
Boolean value | True, False |
You can check a variable’s type by using the type() function:
age = 25
print(type(age))
# Output: <class 'int'>
This indicates that the type of the variable is an integer. Often times, performing this check at various stages of your program minimises time spent down the line to resolve bugs based on a wrong assumption of the type of the variable.
You can also convert between data types using casting functions. This often requires the use of the type name and parenthesis. The following examples show how to achieve this:
age = "25" Now a string
age = int(age) # Now it is an integer
pi = float("3.14")
name = str(123)
However, use with caution, as not all values are convertible. For instance, you cannot convert a string to an integer like below
int("hello") – this will raise an error
print() and type() FunctionsThese are your most familiar tools as a beginner. Their functions are explained below:
print() prints out the content of a variable onto the terminaltype() returns the type of a given variableHere are a few examples:
name = "Charles"
age = 25
print("Hello", name)
print(type(name)) # str
You can also use formatted strings (called f-strings) This allows you to insert variables within normal strings and eases string concatenation:
print(f"{name} is {age} years old.")
# prints "Charles is 25 years old"
You would have been shown some demonstration on how to write Python code. Now, try these tasks on your own to improve your understanding:
print() statement and with f-strings.type() statement to display the type of each variable.We have reached the end of this lesson. You should have better understanding of the following concepts:
int, float, str, and bool.print() and type() to explore your code and debug.In the next lesson, we shall cover Input, Operators & Basic Logic in Python
Not a member yet? Register now
Are you a member? Login now