Introduction to PYTHON

 

Introduction to PYTHON

You can test your Python codes through Online Python editors. You can use it by opening it from any browser.


Python is a very popular programming language and can be used for many purposes. Some of the most used commands related to Python are:


- **print()**: This command is used to print text or numbers on the screen. For example,


`print("Hello world!")`

command, "Hello world!" prints the text on the screen.


-** input() ** : function is used to receive data from the user. This function returns the data entered by the user from the keyboard as a string. The usage of the input() function is as follows:


name = input("Enter your name: ")
print("Hello, " + name)


This code asks the user to enter a name and prints the entered name on the screen. Since the input() function returns the data entered by the user as a string, int() or float() functions can be used to convert the data entered by the user into another data type such as integer or float. For example:


age = input("Enter your age: ")
age_int = int(age)
print("Your age: " + str(age_int))

This code asks the user to enter an age, converts the entered age to integer and prints it on the screen.


-** str **: command represents the string data type in Python. A string is a sequence of data consisting of one or more characters and is defined in Python using single quotes ('...') or double quotes ("..."). Since the String data type is immutable once created, the value of a string cannot be changed.


my_string ="Hello, world!"
print(my_string)

The -**int**: command is used to convert a number or a string to a decimal number in Python. The int() function converts a number or string to a decimal number. For example:


age = int("25")
print(age)

This code converts the string "25" to decimal and prints it to the screen.


- **if/else**: These commands are used to create conditionals. For example, the


`if x > 5: 
print("x is greater than 5")`

command checks whether x is greater than 5 and, if so, prints "x is greater than 5".


-**elif**: command is a combination of else and if commands. For example:

age = 65
if age < 18:
    print("You are under 18")
elif age >= 18 and age < 66: # else if age >= 18 and age < 66:
    print("You are young")
elif age >= 66 and age < 79: # else if age >= 66 and age < 79:
    print("You are middle aged")
elif age >= 80 and age < 100: # else if age >= 80 and age < 100:
    print("You are old")
else:
    print("We couldn't determine your age group!")

- **for/while**: These commands are used to create loops. For example,


 `for i in range(5): print(i)` 

The command prints numbers from 0 to 4 on the screen.


-** while True **: statement is used to create an infinite loop. This loop runs continuously rather than under a specific condition. The `break` statement can be used to end the loop. For example, the following block of code creates a number guessing game that asks the user to guess a number and keeps guessing until they guess the correct number:



```python
import random

number = random.randint(1, 100)

while True:
    guess = int(input("Guess a number between 1 and 100: "))
    if guess == number:
        print("Congratulations, correct guess!")
        break
    elif guess < number:
        print("Guess a higher number.")
    else:
        print("Guess a lower number.")
```

This code checks the number the user guessed by using an `if` block within a `while True` loop. If the user's guess is correct, the loop is terminated using the 'break' statement and the game ends. Otherwise, the user is told to guess a higher or lower number and the cycle continues.


- **list/dict**: These commands are used to create lists and dictionaries. For example, the command `my_list = [1][2][3]` creates a list of the numbers 1, 2, and 3.


-**list[ ]**: command is used to keep a series of data together. Lists consist of comma-separated items enclosed in square brackets. For example:


my_list = [1, 2, 3, 4, 5]
print(my_list)

This code creates a list [1][2][3][4][5] and prints it to the screen. Many operations can be performed on lists, such as adding or removing elements, sorting elements, checking the existence of elements, or creating sublists. For example:


my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list)

This code adds element 6 to my_list using the append() method and prints it to the screen


- **def**: This command is used to create functions. For example,


`def square(x): return x * x`

The command creates a function that calculates the square of a number.


An example code might include an example such as the game X.O.X. This game is a board game where the user plays against the computer. The game board is a 3x3 square, and the user and the computer take turns placing X or O marks in the empty cells on the board. The winner of the game is the player who places three X's or O's on the board side by side, diagonally or vertically.


```python
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

def print_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print("-+-+-")
    print(board[3] + "|" + board[4] + "|" + board[5])
    print("-+-+-")
    print(board[6] + "|" + board[7] + "|" + board[8])

def check_win(player):
    if board[0] == player and board[1] == player and board[2] == player:
        return True
    elif board[3] == player and board[4] == player and board[5] == player:
        return True
    elif board[6] == player and board[7] == player and board[8] == player:
        return True
    elif board[0] == player and board[3] == player and board[6] == player:
        return True
    elif board[1] == player and board[4] == player and board[7] == player:
        return True
    elif board[2] == player and board[5] == player and board[8] == player:
        return True
    elif board[0] == player and board[4] == player and board[8] == player:
        return True
    elif board[2] == player and board[4] == player and board[6] == player:
        return True
    else:
        return False

def play_game():
    print("Welcome to X.O.X Game!")
    print_board()
    while True:
        # User's move
        user_move = int(input("Please select a move (1-9): ")) - 1
        if board[user_move] != " ":
            print("This cell is full. Please select another cell.")
            continue
        board[user_move] = "X"
        print_board()
        if check_win("X"):
            print("Congratulations, you won!")
            break
        if " " not in board:
            print("The game ended in a draw.")
            break
        # Computer's move
        computer_move = None
        for i in range(9):
            if board[i] == " ":
                computer_move = i
                break
        board[computer_move] = "O"
        print("Computer's move:")
        print_board()
        if check_win("O"):
            print("Unfortunately, you lost.")
            break

play_game()
```

This sample code can be used to play the X.O.X game. The game board is defined as a list called `board` and this list is updated with each move. `print_board()` function is used to print the board to the screen. The `check_win(player)` function checks whether a player has won the game. The `play_game()` function is used to play the game and takes the user's moves and calculates the computer's moves. The game continues until one player wins or the board is full.


Citations:

[1] https://python-istihza.yazbel.com/listeler_ve_demetler.html

[2] https://python-istihza.yazbel.com/kosul_deyimleri.html

[3] https://hakanyalcinkaya.github.io/python/001-python-ve-kod-yazma-sanati.html

[4] https://ekrendersler.medium.com/pythona-giri%C5%9F-part-1-e2e7103ba343

[5] https://fatiherikli.medium.com/y%C4%B1lan-hikayesi-i%CC%87lk-b%C3%B6l%C3%BCm-869f212bb1a2

[6] https://www.pythonkod.com/yazbel-python/

Crow

physics, information technologies, author, educator

Post a Comment

Hello, share your thoughts with us.

Previous Post Next Post

İletişim Formu