Day 2: Basic Syntax and Data Types
Python is an interpreted, high-level, and general-purpose programming language that is easy to learn and has a clean syntax. The language was created by Guido van Rossum and released in 1991. Python is often used for web development, scientific computing, data analysis, artificial intelligence, and automation.
Basic Syntax
In Python, the code is executed line by line. A line of code is terminated by a newline character. The semicolon character is not used to terminate a line of code in Python. Indentation is used to define blocks of code. Python has a simple syntax, and its statements are usually one-liners.
Variables and Constants
Variables and Constants
In Python, a variable is a name that refers to a value, and it can be used to store data values. The value that a variable refers to can be of any data type, such as an integer, a float, a string, or a Boolean. When you declare a variable in Python, you do not need to specify its data type explicitly, as the type is inferred from the value that you assign to it.
Here’s an example of how to declare and initialize a variable in Python:
x = 10
In this example, we’re declaring a variable x and initializing it to a value of 10. The equal sign = is used to assign a value to a variable. This means that the value 10 is now associated with the name x.
A constant is a variable whose value cannot be changed during the execution of a program. In Python, there is no way to declare a variable as a constant explicitly. However, by convention, you can use all-uppercase letters to indicate that a variable should be treated as a constant, and its value should not be changed.
For example:
TAX_RATE = 0.1
In this example, we’re declaring a constant TAX_RATE and initializing it to a value of 0.1. The value of this constant should not be changed during the execution of the program.
By using variables and constants, you can store and manipulate data in your Python programs. Variables and constants are fundamental concepts in programming and are used in almost every program.
Data Types
In Python, data types define the type of data that a variable can store. Here are the built-in data types in Python:
- Numeric data types (int, float, complex)
- String data type
- Boolean data type
- None data type
Numeric Data Types
Python has three numeric data types:
- int: for integer values
- float: for decimal values
- complex: for complex numbers with a real and imaginary part
Here’s an example of declaring and initializing variables of each numeric data type:
x = 5 # int
y = 2.5 # float
z = 3 + 4j # complex
String Data Type
A string is a sequence of characters, enclosed in quotation marks. Python supports single quotes (‘…’) and double quotes (“…”) for defining strings. Strings are immutable, which means that once you create a string, you cannot change it.
Here’s an example of declaring and initializing a string:
s = "Hello, world!"
Boolean Data Type
A boolean is a data type that has only two possible values: True and False. Booleans are often used in conditional statements to control the flow of a program.
Here’s an example of declaring and initializing a boolean variable:
b = True
None Data Type
The None data type represents a null or undefined value. It is often used to initialize variables or as a placeholder for a value that will be set later.
Here’s an example of declaring and initializing a None variable:
coden = None
Type Conversion
In Python, you can convert one data type to another using type conversion. There are two types of type conversion: implicit and explicit. Implicit type conversion is done automatically by the interpreter, while explicit type conversion is done manually using built-in functions.
Here’s an example of how to convert a string to an integer using explicit type conversion:
num_str = "10"
num_int = int(num_str)
In this example, we’re converting the string “10” to an integer using the int() function.
Printing output is a fundamental concept in any programming language, including Python. In Python, you can use the print() function to display output to the console. You can print variables, constants, and expressions.
The syntax of the print() function is straightforward. It takes one or more arguments and displays them on the console. Here’s an example of how to print a string to the console:
print("Hello, world!")
In this example, we’re printing the string “Hello, world!” to the console.
You can also print variables by passing them as arguments to the print() function. Here’s an example:
message = "Welcome to Python!"
print(message)
In this example, we’re printing the value of the message variable to the console.
Sometimes you may need to print multiple values in the same line. You can do that by separating the values with commas. Here’s an example:
name = "Alice"
age = 25
print("My name is", name, "and I'm", age, "years old.")
In this example, we’re printing multiple values in the same line.
You can also format the output using the print() function. There are several ways to format the output, but one of the most popular methods is to use the format() method. Here’s an example:
name = "Alice"
age = 25
print("My name is {} and I'm {} years old.".format(name, age))
In this example, we’re using the format() method to format the output.
That’s it for this Lesson on Basic Syntax and Data Types in Python. Stay tuned for more lessons on Python programming!
Exercise
- Write a Python program that asks the user to enter two numbers and stores them in separate variables.
- Print the sum of the two numbers.
- Print the difference between the two numbers.
- Print the product of the two numbers.
- Print the quotient (result of division) of the two numbers.
- Print the remainder of the division of the two numbers.
- Convert the first number to a string and concatenate it with the second number as a string. Print the resulting string.
- Convert the second number to an integer and add it to the first number. Print the sum.
- Convert the second number to a float and multiply it by the first number. Print the product.
- Write a Python program that asks the user to enter a string of numbers separated by commas, and stores the string in a variable.
- Convert the string to a list of integers and print the resulting list.
- Convert the list of integers back to a string and print the resulting string.
Note: You can use the input() function to get user input, and the int(), float(), str() functions for type conversion.