Содержание

## How to learn python for absolute beginners?

There are a large number of absolute beginner Python projects published online. You probably came across something fancy and thought, “Hmm… this is so cool! I want to make it, too!”. Great! And congratulations, you have come to the right place.

There are several simple steps that you should try if you want to get a head start in your Python programming journey.

On pythongui.org, you can find plenty of resources about Python programming, especially if you are an absolute beginner. Whether you want to make Python GUIs for any operating system or you have particular questions about using Python, head over to this category full of free resources.

## What are the must-have tools for Python coding?

It is important to be efficient and have all the needed plugins to make coding smooth. Here are some features that a Python IDE must definitely have. They include basic features like syntax highlight, spellchecking, code alignment and autocomplete. Anybody can make a typing error that only becomes visible when you write the code. Something as simple as *imprt* instead of *import* can cause your code to crash. Word processors highlight and even automatically correct such obvious mistakes. This should definitely be the case in your programming environment.

## What are the Python essentials?

You need to learn some basic programming concepts such as working with variables, if/else clauses, loops, functions and more. It is beyond the scope of this article to explain them in detail, but there are plenty of resources online, including the documentation on python.org. We recently published a quick overview, too. Remember to put as many comments in the source code as possible. Your future self will appreciate the effort. You will forget what you were thinking a month from now. It will save you tons of time to actually know what you had in mind. Also, it makes it easier to share your code with another developer.

## Do you need to find the motivation?

Sounds simple, right? Well, in the very beginning, you imagine that it will be a stroll in the park to get everything about your project done right away. After all, everybody says Python is easier than other programming languages. While being true, the easiness should not be overestimated. There will be challenges along the way, and sometimes you will get stuck. That is why you need to learn problem-solving. If you keep on getting cryptic error messages, then you should search online. Chances are that somebody else has come across the same, too. Many beginners make similar mistakes, after all. There are countless resources online to read more and even ask for help. Like anything, learning Python will reach a point in the learning curve where it feels like you are not progressing anywhere. This is perfectly normal. Just keep going and pushing.

## Do you need any ideas on what projects to start with?

So, to keep your motivation running, here are five project ideas to try out.

- Dice rolling
- Calculator
- Binary search
- Rock, Paper, Scissors
- Random password Generator

### A simple dice rolling Python example

After “Hello World”, this is probably the simplest program one can write. The task is to generate a unique number between 1 and 6. Note that the numbers produced by the *randint* function are pseudorandom.

1 2 3 |
import random n = random.randint(1,6) print(n) |

### Calculator Python example

Next, we will make a simple calculator. It takes two numbers and an operator for either addition, subtraction, multiplication or division. The user will be asked to enter the first number, then the operator, followed by the second number. There are alternative ways of constructing the algorithm, e.g., the user enters the whole string – both numbers and operator at once. The string then is split, and the operator is identified. This requires more safety checks, so to keep things simple, here we will do it in three steps.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
N1 = input("Enter a number:") # convert the string to an integer N1 = float(N1) op = input("Enter an operator (+, -, * or /): ") N2 = input("Enter the second number:") # convert the string to an integer N2 = float(N2) if (op == "+"): print(N1, "+", N2, "=", N1+N2) elif (op == "-"): print(N1, "-", N2, "=", N1-N2) elif (op == "*"): print(N1, "*", N2, "=", N1*N2) elif (op == "/"): print(N1, "/", N2, "=", N1/N2) else: print("Invalid operator") |

### Rock, Scissors, Paper game in Python for the absolute beginner

This simple game requires that the computer choose one option, the user enter another, and then they are compared according to the rules:

- Rock breaks scissors
- Scissors cut paper
- Paper covers rock

There are three states: 0 – rock, 1 – scissors, and 2 – paper. The computer will make its choice by generating a random number between 0 and 2. The user will enter one of these numbers.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import random # The three states are: # 0 - rock # 1 - scissors # 2 - paper # the computer chooses a number pc = random.randint(0,2) # the user enters a number print("Choose your option:") print("0 - rock") print("1 - scissors") print("2 - paper") user = input("") # convert the string to an integer user = int(user) # make a sanity check if (user < 0) or (user > 2): print("Wrong input") exit() print("The computer chose: ",end="") if (pc == 0): print("rock") elif (pc == 1): print("scissors") elif (pc == 2): print("paper") print("The user chose: ",end="") if (user == 0): print("rock") elif (user == 1): print("scissors") elif (user == 2): print("paper") # decide who wins if (pc == 0): if (user == 0): print("Tie game") exit() elif (user == 1): print("Computer wins") exit() elif (user == 2): print("User wins") exit() elif (pc == 1): if (user == 0): print("User wins") exit() elif (user == 1): print("Tie game") exit() elif (user == 2): print("Computer wins") exit() elif (pc == 2): if (user == 0): print("Computer wins") exit() elif (user == 1): print("User wins") exit() elif (user == 2): print("Tie game") exit() |

### A Python random password generator

Now something useful. Let’s generate a secure password, employing small and capital letters, numbers and characters. First, the user enters the length of the password. There are various ways to proceed afterwards. Here, I will show an easier-to-understand but less efficient method. I have defined all characters in a list. Then, I will generate random numbers that correspond to the indices of the characters in the list and concatenate them.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import random chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', '?', '.', ','] N = input("Enter the length of the password: ") # convert the string to an integer N = int(N) # initialise an empty string password = '' # add all characters one by one for i in range(0,N): index = random.randint(0,len(chars)-1) password = password + chars[index] print('Your password is') print(password) |

### How to perform a binary search in Python

Binary search is a simple algorithm that comes in handy in various advanced projects. For example, it can be used to find the index of a number in an array of unique numbers. The problem is tackled by sorting the numbers by size and then checking whether the number of interest is smaller or larger than the middle element.

For example, if an array of 10 elements contains the numbers [ 5 8 33 36 48 58 60 81 89 98], and we want to check what is the index of 89, we need to split the range into two sub-intervals: the first five elements in the ordered array is the “left interval”. It contains the numbers [ 5 8 33 36 48]. The other five elements are the ”right interval”: [58 60 81 89 98]. We find that 89 belongs to the right interval and repeat the procedure by splitting the sub-interval into sub-sub-intervals. The left interval becomes [58,60], and the right interval becomes [89,98]. Again, 89 is in the right interval. We split again and find that 89 is in the left interval, i.e., the only member of this interval. We have been keeping track of the indices all the time, so we can tell that its position was 8. In Python, index counting starts at 0.

Here is the suggested solution.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import numpy as np import random from math import floor ARR_SIZE = 10 def main(): # generate an array # array = np.random.randint(100, size=(ARR_SIZE)) # generate an array of unique numbers between 1 and 100 array = random.sample(range(1, 100), ARR_SIZE) # order the array array = np.sort(array) print(array) # read in the number to search for number = input("Enter a number between 0 and 100: ") # convert the string to an integer number = int(number) # initialise the indices of the boundaries of the interval left = 0 right = ARR_SIZE - 1 # run the algorithm while (left <= right): # find the index of the middle element in the interval # round down to avoid fractional indices mid = floor((left + right)/2) # compare the number to the middle element if (number > array[mid]): print("The number is in the right interval:") print(array[mid+1:right+1]) left = mid + 1 elif (number < array[mid]): print("The number is in the left interval:") print(array[left+1:mid+1]) right = mid - 1 else: print("The number is in position:", mid) break if __name__ == '__main__': main() |

### If all of these felt simple, you can try a challenge – tic-tac-toe.

Download now PyScripter to kickstart your journey to Python mastery.