Basics Python
Python is one of the most popular programming languages in the world. It is known for its simple syntax, making it a great language for beginners to learn. In this article, we will cover some of the basic elements of Python, including data types, variables, and control structures.
In the
example above, we created a variable called x and assigned it the value
of 5. We then used the print() function to print the value of x
to the console, which is 5.
In Python,
you can assign values to variables using the assignment operator =. You
can also use the assignment operator to change the value of a variable. For
example:
In this
example, we first assigned the value of 5 to the variable x. Then we used the
assignment operator to change the value of x to x + 2, which is 7. We then
printed the value of x to the console and it shows 7.
It's also
important to note that Python is a loosely typed language, so you don't need to
specify the data type of a variable when you create it. The data type of a
variable is determined by the value that is assigned to it. For example:
You can
also use the built-in type() function to check the data type of a variable. For
example:
This will return the type of the variable x as 'str'
It's also worth noting that variables in Python follow the naming
conventions :
- · the names can contain letters, digits, and underscores.
- · the names cannot start with a digit.
- ·
Python reserves some words as keywords, which cannot
be used as variable names.
In summary, variables are an essential element of the Python language and
are used to store and manipulate data in a program. They can be created and
assigned values using the assignment operator =, and their data type is
determined by the value that is assigned to them. Understanding how to create
and use variables is a key step in becoming proficient in Python.
In Python,
you can use the input() function to get input from the user. The input()
function takes a string as an argument, which is displayed as a prompt to the
user. The user can then enter a value, which is returned as a string by the input()
function.
For example, here's how you can use the input() function to get a number from the user:
In this
example, the string "Enter a number: " is displayed as a prompt to
the user, and the user can enter a number. The value entered by the user is
then stored in the variable x as a string.
It's
important to note that the input() function always returns a string, so if you
need to use the input as a number, you'll need to convert it to the appropriate
data type. For example, if you want to use the input as an integer, you can use
the int() function:
An if
statement is used to check if a certain condition is true, and if it is, a
block of code will be executed. The basic structure of an if statement is as
follows:
For
example, this is how you can use an if statement to check if a variable is
greater than 5:
In this
example, the condition is x > 5, and since the value of x is 10, which is
greater than 5, the code inside the if block print("x is greater than
5") will be executed and the output will be "x is greater than
5"
An if-else
statement is used to check if a certain condition is true, and if it is, a
block of code will be executed, otherwise, a different block of code will be
executed. The basic structure of an if-else statement is as follows:
For example, this is how you can use an if-else statement to check if a
variable is greater than 5:
In this
example, the condition is x > 5, and since the value of x is 3, which is not
greater than 5, the code inside the else block print("x is not greater
than 5") will be executed and the output will be "x is not greater
than 5"
An if-elif-else
statement is used to check multiple conditions. You can chain multiple elif
statement after the if statement and it will check the condition for each elif
statement in the order they are written. If any of the conditions is true, the
corresponding block of code will be executed and the control will exit the if-elif-else
block. The basic structure of an if-elif-else statement is as follows:
For
example, this is how you can use an if-elif-else statement to check the value
of a variable and output a message accordingly:
In this
example, the first condition x > 20 is false, second condition x > 15 is
true, so the code inside the elif block print("x is greater than 15")
will be executed and the output will be "x is greater than 15"
It's
important to note that only one block of code from the if, elif, and else will
be executed and once a condition is true, the control exits the if-elif-else
block.
In summary,
if statements are used to check if a certain condition is true and if it is, a
block of code will be executed. if-else statements are used to check if a
certain condition is true and if it is, a block of code will be executed,
otherwise, a different block of code will be executed. if-elif-else statements
are used to check multiple conditions and depending on the result of these
conditions, a corresponding block of code will be executed. These statements
are powerful tools for controlling the flow of a program and make it possible
to create more complex logic.
A for
loop is used to iterate through a sequence of items, such as a list or a
string. The basic structure of a for loop is as follows:
The sequence
can be any iterable object in python such as a list, tuple, string, etc. The variable
is a temporary placeholder that holds the current value of the sequence during
each iteration of the loop.
For
example, here's how you can use a for loop to print out all the items in a
list:
In this
example, the loop iterates over each item in the list fruits, assigns it to the
variable fruit, and then prints it out. This will output:
A while
loop, on the other hand, is used to repeatedly execute a block of code as long
as a certain condition is true. The basic structure of a while loop is as
follows:
As long as
the condition is true, the code inside the loop will continue to execute. Once
the condition becomes false, the loop will exit and the program will continue
with the next line of code.
For
example, here's how you can use a while loop to repeatedly print out the
numbers from 0 to 4:
In this
example, the loop starts with the variable x set to 0. As long as x is less
than 5, the loop will continue to execute and print out the current value of x.
The variable x is then incremented by 1 after each iteration. This will
output:
It's
important to note that if the condition in a while loop is always true, the
loop will run indefinitely, resulting in an infinite loop. So it's important to
make sure that the condition will eventually become false, otherwise it will
never exit the loop.
In summary,
for loops are used to iterate through a sequence of items, while while loops
are used to repeatedly execute a block of code as long as a certain condition
is true. Both for and while loops are powerful tools for controlling the flow
of a program and can be used in a wide variety of situations. Understanding how
to use these loops is an important step in becoming proficient in Python.
In Python, a module is a file containing Python definitions and statements.
The file name is the module name with the suffix .py added. For example, random
is a module that implements pseudo-random number generators for various uses
and distributions. max() and min() are also built-in functions in python and
they are used to return the largest and smallest item in an iterable or the
largest and smallest of two or more arguments respectively.
The random module provides functions that generate pseudo-random numbers.
These functions are not truly random, as they are generated by an algorithm,
but they are good enough for most purposes. Some of the most useful functions
in the random module include:
- random.randint(a, b): This function returns a random integer between a and b, including both a and b.
- random.uniform(a, b): This function returns a random floating-point number between a and b.
- random.choice(iterable): This function returns a random item from an iterable.
- random.shuffle(iterable): This function shuffles items in an iterable.
It's also worth mentioning that python has a built-in module math which
provides mathematical operations like square root, trigonometric functions and
many more.
You can use these modules by importing them at the beginning of your
script. For example, to use the random module, you would add the following line
at the top of your script:
Using random.random():
Using random.randrange(start,
stop, step):
Using math.pi
and math.e:
Using math.ceil(x)
and math.floor(x):
Using min(iterable)
and max(iterable):
You can
also use min() and max() with multiple arguments:
You can use
these functions and methods by importing the module and calling the
function/method with the appropriate arguments. It's worth noting that you can
also use the help() function to get more information about the functions and
methods provided by a module.
Now that you've learned the basics of Python, it's time to put your new knowledge to the test! Below are a list of exercises that will help you solidify your understanding of the concepts covered in this article. Give them a try and see how much you've learned! Remember, practice makes perfect and keep experimenting with different examples to improve your python skills.
QUESTIONS :
- Write a program that prompts the user to enter three numbers, and then prints out the largest number.
- Write a program that prompts the user to enter a string, and then checks if it is a palindrome (a word or phrase that reads the same forwards as backwards).
- Write a program that generates a random list of numbers and prompts the user to enter a number, then it will display the number of even numbers and the number of odd numbers in that list.
- Write a program that prompts the user to enter a number, and then displays the multiplication table for that number (e.g. if the user enters 5, the program will display "5 x 1 = 5, 5 x 2 = 10, etc").
- Write a program that prompts the user to enter a word, and then displays the number of occurrences of each letter in that word
Komentarze
Prześlij komentarz