What is Python
Python is a high-level, interpreted programming language that is known for its simplicity, readability, and ease of use. It is used for a wide range of applications, including web development, data analysis, artificial intelligence, and automation. Python's syntax is designed to be easy to understand, which makes it a popular choice for beginners and experts alike. One of Python's strengths is its vast collection of libraries and frameworks, which provide a wide range of tools for developers to use in their projects.
Installing Python
To install Python on your machine, you can go to the official Python website and download the appropriate version for your operating system. Once downloaded, follow the installation instructions and Python will be ready to use on your machine.
Version | Operating System | Description | MD5 Sum | File Size | GPG |
---|---|---|---|---|---|
Source release | 38c84292658ed4456157195f1c9bcbe1 | 17539408 | SIG | ||
Source release | fd6cc8ec0a78c44036f825e739f36e5a | 12854736 | SIG | ||
for OS X 10.9 and later | ce98eeb7bdf806685adc265ec1444463 | 24889285 | SIG | ||
Windows | 20b111ccfe8d06d2fe8c77679a86113d | 25178278 | SIG | ||
Windows | bb0897ea20fda343e5179d413d4a4a7c | 26005670 | SIG | ||
Windows | b3b753dffe1c7930243c1c40ec3a72b1 | 6322188 | SIG | ||
Windows | for AMD64/EM64T/x64 | a425c758d38f8e28b56f4724b499239a | 20598784 | SIG | |
Windows | db6ad9195b3086c6b4cefb9493d738d2 | 19632128 | SIG |
Version | Operating System | Description | MD5 Sum | File Size | GPG | Sigstore |
---|---|---|---|---|---|---|
Source release | 1aea68575c0e97bc83ff8225977b0d46 | 26006589 | SIG | CRT | ||
Source release | b8094f007b3a835ca3be6bdf8116cccc | 19618696 | SIG | CRT | ||
macOS | for macOS 10.9 and later | 4c89649f6ca799ff29f1d1dffcbb9393 | 40865361 | SIG | CRT | |
Windows | 7e4de22bfe1e6d333b2c691ec2c1fcee | 7615330 | SIG | CRT | ||
Windows | 7f90f8642c1b19cf02bce91a5f4f9263 | 8591256 | SIG | CRT | ||
Windows | 643179390f5f5d9d6b1ad66355c795bb | 9355326 | SIG | CRT | ||
Windows | 58755d6906f825168999c83ce82315d7 | 27779240 | SIG | CRT | ||
Windows | Recommended | bfbe8467c7e3504f3800b0fe94d9a3e6 | 28953568 | SIG | CRT |
Variables
Variables are an essential concept in programming, and Python is no exception. In Python, a variable is a container that stores a value, such as a number, string, or list. These values can be changed or manipulated throughout a program, making variables a powerful tool for building complex applications. In this section, we will explore how to declare and use variables in Python and learn about different data types that can be stored in a variable.
In Python, there are several variable types, including:
- Integers (int)
- Floating-point numbers (float)
- Booleans (bool)
- Strings (str)
- Lists (list)
- Tuples (tuple)
- Dictionaries (dict)
- Sets (set)
Variables can also be assigned the value of None, which represents the absence of a value. Here is an example of Integer, Float, and String variable in Python.
# Declare a variable 'age' and assign it the value 25
age = 25
# Declare a variable 'y' and assign it the value 3.14
y = 3.14
# Declare a variable 'name' and assign it the value "John"
name = "John"
# Print the value of 'age'
print(age)
# Print the value of 'y'
print(y)
# Print the value of 'name'
print(name)
In this example, we declare three variables: age
, y
, and name
. We assign the value 25
to age
, the value 3.14
to y
, and the value "John"
to name
. We then print the values of age
, y
, and name
to the console using the print()
function.
The output of the program will be:
25
3.14
John
This demonstrates how to declare and use variables in Python, and shows some of the different data types that can be stored in a variable (integer, float, and string).
Strings
In Python, a string is a sequence of characters enclosed in quotation marks. Python allows strings to be enclosed in either single quotes ('...') or double quotes ("..."). Strings can be used to represent text, numbers, or other types of data.
Here are some examples of how to declare and use strings in Python:
# Declare a string variable 'name' and assign it the value "Alice"
name = "Alice"
# Declare a string variable 'message' and assign it the value "Hello, world!"
message = 'Hello, world!'
# Print the value of 'name'
print(name)
# Print the value of 'message'
print(message)
# Concatenate two strings
greeting = "Hello"
name = "Alice"
full_greeting = greeting + " " + name
print(full_greeting)
# Access individual characters in a string
word = "Python"
first_letter = word[0]
last_letter = word[-1]
print(first_letter)
print(last_letter)
In this example, we declare two string variables: name
and message
. We assign the value "Alice"
to name
and the value "Hello, world!"
to message
. We then print the values of name
and message
to the console using the print()
function.
We also show how to concatenate two strings using the +
operator, and how to access individual characters in a string using indexing. Finally, we print the first and last characters of the string "Python"
.
The output of the program will be:
Alice
Hello, world!
Hello Alice
P
n
This demonstrates how to declare and use strings in Python, including how to concatenate strings and access individual characters in a string using indexing.
Here are some commonly used string methods in Python:
count(substring)
: Returns the number of occurrences of a substring in a string. Example:"hello world".count("l")
will return3
.find(substring)
: Returns the index of the first occurrence of a substring in a string, or1
if the substring is not found. Example:"hello world".find("l")
will return2
.upper()
: Returns the string in all uppercase letters. Example:"hello world".upper()
will return"HELLO WORLD"
.lower()
: Returns the string in all lowercase letters. Example:"HELLO WORLD".lower()
will return"hello world"
.replace(old, new)
: Replaces all occurrences of a substring in a string with a new substring. Example:"hello world".replace("l", "L")
will return"heLLo worLd"
.split(delimiter)
: Splits a string into a list of substrings based on a delimiter. Example:"hello,world".split(",")
will return["hello", "world"]
.
These are just a few examples of the many methods available for manipulating strings in Python.
Data Structure
Conditional Statement
In Python, conditional statements are used to execute different parts of code depending on the truth value of a condition. The most common conditional statements in Python are if
, elif
, and else
.
if statement
The if
statement is used to execute a block of code if a condition is true. Here is the syntax for the if
statement:
if condition:
# code to execute if condition is true
The code block under the if
statement is executed only if the condition is true. If the condition is false, the code block is skipped.
Here is an example:
age = 18
if age >= 18:
print("You are old enough to vote.")
In this example, the if
statement checks if the age
variable is greater than or equal to 18
. If it is, the message "You are old enough to vote."
is printed to the console.
elif statement
The elif
statement is used to check for additional conditions after the if
statement. Here is the syntax for the elif
statement:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
The code block under the elif
statement is executed only if the if
statement is false and the elif
condition is true. If the elif
condition is false, the code block is skipped.
Here is an example:
age = 16
if age >= 18:
print("You are old enough to vote.")
elif age >= 16:
print("You can drive, but you cannot vote.")
In this example, the if
statement checks if the age
variable is greater than or equal to 18
. If it is, the message "You are old enough to vote."
is printed to the console. If the if
statement is false, the elif
statement checks if the age
variable is greater than or equal to 16
. If it is, the message "You can drive, but you cannot vote."
is printed to the console.
else statement
The else
statement is used to execute a block of code if all the previous conditions are false. Here is the syntax for the else
statement:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
else:
# code to execute if all conditions are false
The code block under the else
statement is executed only if all the previous conditions are false.
Here is an example:
age = 14
if age >= 18:
print("You are old enough to vote.")
elif age >= 16:
print("You can drive, but you cannot vote.")
else:
print("You are not old enough to drive or vote.")
In this example, the if
statement checks if the age
variable is greater than or equal to 18
. If it is, the message "You are old enough to vote."
is printed to the console. If the if
statement is false, the elif
statement checks if the age
variable is greater than or equal to 16
. If it is, the message "You can drive, but you cannot vote."
is printed to the console. If both the if
and elif
statements are false, the else
statement prints the message "You are not old enough to drive or vote."
to the console.
These are just a few examples of how conditional statements can be used in Python. They can be combined in many different ways to create complex programs.
Switch Statement
Python does not have a switch
statement like some other programming languages, but the same functionality can be achieved using if
and elif
statements.
Here is an example of how to use if
and elif
statements to achieve the same functionality as a switch
statement:
def switch_case(case):
if case == 1:
print("Case 1")
elif case == 2:
print("Case 2")
elif case == 3:
print("Case 3")
else:
print("Invalid case")
switch_case(1) # prints "Case 1"
switch_case(2) # prints "Case 2"
switch_case(3) # prints "Case 3"
switch_case(4) # prints "Invalid case"
In this example, the switch_case()
function takes a single argument case
. The function uses if
and elif
statements to check the value of case
and execute the corresponding block of code. If case
does not match any of the specified values, the function prints "Invalid case"
.
This demonstrates how to achieve the same functionality as a switch
statement in Python using if
and elif
statements.
Differences between if condition and switch condition
An "if"
condition is a conditional statement that executes a block of code if a specified condition is true. A "switch"
condition is a conditional statement that evaluates an expression and executes code as per the matched case label. While "if"
conditions are generally used for simple, straightforward comparisons, "switch"
conditions can be more useful for complex comparisons or when multiple cases need to be evaluated.
Please Excuse My Dear Aunt Sally [order of operation in Python and called PEMDAS]
() Parentheses
^ Exponent
x Multiplication
/ Division
+ Addition
- Subtraction
Examples:
(1 + 2) * 3 ⇒ 6
( True or True ) and False ⇒ False