Python Programming Language - First Python Programme
We will begin with a very basic first programme in order to understand the syntax of Python. When learning a new programming language, it is customary to begin with a Hello World application. "HELLO WORLD!" A script that prints "Hello World!" to the screen is all that a Hello World application is. This is especially easy to do in Python. "Hello World!" is printed. This is a Python one-liner, as you can see. To print a single text, we would need to design a fundamental structure in another language, complete with functions, classes, and more. However, let's investigate this situation. The so-called function with the name print is the first thing that stands out. When we utilise that function, a specific text is displayed on the screen. We must place the text we want to print between the brackets. The use of quote marks is another crucial component in this. They serve as a reminder that the text is a string and not the name of another object. A data type that represents text is a string. Without quotation marks, the interpreter will believe that Hello World! is a variable name rather than a text and will behave accordingly. Consequently, we will receive an error message.
0 Comments
Python Programming Language - Conditions Up until this point, the translator has carried out one command at a time. Conditions affect how this changes. IF, ELSE, IF In order for our script to execute the code in its block, a condition essentially needs to return True. If, elif, and else are the three crucial keywords in this sentence. The user enters a number that this script converts to an integer. When this number is less than ten, our first if statement determines if it is. Always keep in mind that comparisons only yield True or False. The code that is indented here is run if the return value is True. Python code blocks are identified with colons and indentations. If this condition returns False, the elif-block is reached and this condition is checked there. Here, the identical process takes place. Elif blocks can be as numerous as you like. If neither criterion is true, we enter the else-block. You can see how these fundamental if, elif, and else trees function in this flowchart. Of course, an elif or else block is not necessary. Simply create an if-statement and the rest of the script will run without the code if the condition is not met. NESTED IF-STATEMENTS
You can also put if-blocks into if-blocks. These are called nested if-statements.i f number % 2 == 0: if number == 0: print("Your number is even but zero") else: print("Your number is even") else: print("Your number is odd") So, here we have the first condition, which checks if the number is even. When it’s even it then checks if it’s a zero or not. That’s a trivial example but you get the concept. Python Programming Language-User Input
We have merely printed text onto the screen up until this point. However, we also have the option of entering our own data into the script. In this chapter, we'll examine user input and how to deal with it. Input Function We may retrieve user input from the console application using Python's input method. input = name ("Enter your name here:") print(name) The user's name is saved into the variable name when entered here. This variable can then be called and printed. number1 = input ("Enter first number: ") number2 = input ("Enter second number:") sum = number1 + number2 print("Result: ", sum) This illustration is a little deceptive. It appears that we are printing the sum after receiving two numbers as input. The function input always returns a string, which is a concern. As a result, when you enter 10, the variable's value is "10," which is a string. What occurs when we combine two strings, then? Simply put, we add one to the other. As a result, "15" and "26" would equal "1526" when added together. We must first typecast our variables if we wish to perform a mathematical addition. number1 = input("Enter first number: ") number2 = input("Enter second number: ") number1 = int(number1) number2 = int(number2) sum = number1 + number2 print("Result: ", sum) Our script is now effective. Always keep in mind that the input function returns a string, so if you wish to perform computations with it, you must typecast it.
Operators
The subject of operators is what we'll learn next. To handle variables or values and perform actions on them, we need operators. There are many various kinds of operators, and in this chapter, we'll discuss their variations and uses. Arithmetic Operators Arithmetic operators are the most basic operators. They are presumably familiar to you from maths.
Click ▸ to run the python interpreter below
Assignments Operators
We are also familiar with assignment operators as another category of operators. We use them to provide variables values, as their name already implies. Basically, we assign a value directly using these operators. The two sentences that follow each have the same impact. It's simply easier to write that way.
Click ▸ to run the python interpreter below
Comparison Operators We always obtain a Boolean when we employ comparison operators to compare two items. As a result, our conclusion can either be True or False. When working with conditions and loops, we employ comparisons. We shall discuss these two subjects in more detail in following chapters. A comparison returns True if it is accurate, and False if it is incorrect. Let's examine a few instances.
Click ▸ to run the python interpreter below
Logical Operators
To combine or connect Booleans or comparisons, logical operators are needed. I think this is best explained by examples, so let’s look at some.
Click ▸ to run the python interpreter below
VARIOUS OPERATORS
Other operators include bitwise operators and membership operators. However, some of them are unnecessary, and understanding others requires a little more programming expertise. Python Programming Language – Creating Variables
Python, creating variables is really easy. We simply select a name and give it a value. myNumber = 10 myText = "Hello" We have two variables declared here. One of them is a number, and the other is a string. Although there are some restrictions, you can essentially choose any name you wish. You cannot use reserved keywords like int or dict, for instance. Additionally, other than the underlining, the name cannot begin with a number or other special character. Python Programming Language –Data Types and Variable Data Types and Variables Probably in your math lessons, you have previously come across variables. They are essentially just placeholders for values. That applies to programming as well. The distinction is that we use a wide variety of data types, and variables can now hold values for both complete objects and numeric values. Types of Numerical Data The types of data that you are most likely already familiar with come from mathematics. Numerous types of numbers can be applied to mathematical procedures. It is, as you can see, pretty easy. An integer is simply a regular whole number that we may use to perform simple calculations. A float is a floating point number that extends the integer and allows decimal places. And a complex number is just a number that consists of both real and fictitious elements. Complex numbers should be ignored if you lack mathematical understanding of them. They are not currently required for your programming.
Strings A string is essentially just a text or a basic sequence of characters. Our text was a string when we printed it in the last chapter. Strings must always be included in quotation marks. Otherwise, the interpreter won't understand that they should be read like text. In Python, the word for String is str. Booleans In Python, booleans are most likely the most basic data type. They only have two possible values: True or False. A binary data type, that is. When we come to conditions and loops, we'll utilise it a lot. bool is the crucial word here. Sequences We shall discuss sequences in a subsequent posts. Sequences, though, are also data types, so we'll at least note their existence.
Python Programming Language – Running the Script
Now, all that remains is to execute the script we just wrote. This requires saving the script as a Python file. Then you can utilise the IDLE's integrated interpreter. Simply select Run > Run Module (or F5). Python Programming Language – HELLO WORLD
In order to comprehend the syntax of Python, we will begin with a very simple programme. When learning a new programming language, it is conventional to begin with a Hello World application. A Hello World application is simply a script that displays the text "Hello World!" This is especially simple in Python. print("Hello World!") As can be seen, this is a single-line Python programme. In other programming languages, printing a single text would necessitate defining a basic structure with functions, classes, and more. However, let's observe what is occurring. The very first thing we notice is the so-called function named print. This function outputs specific text to the screen when invoked. The text to be printed must be placed between the parentheses. Additionally, the quotation marks are a crucial element here. They indicate that the text is a string rather than a proper name. A string represents text as a data type. Without quotation marks, the interpreter will consider Hello World! to be a variable name and not a text. Consequently, we will receive an error message. Python Programming Language - PYCHARM
You may also choose to utilise a highly professional IDE with numerous features. For Python, use PyCharm. This development environment was created by JetBrains, a well-known and reputable business. It has a multitude of features, expert syntax highlighting, and an intuitive user interface. I would recommend it to every Python developer, but I believe it may be excessive and unnecessary for novices. However, that is your choice. If you are interested, the community edition is available for free download. Python Programming Language – Editor and CLI
If you prefer to use a particular editor, such as Atom, Sublime, or VS Code, you can execute your code from the command line. Therefore, you compose your code in your editor and save the file. Run CMD (on Windows) or Terminal (on Linux & Mac). You must use the following syntax to execute the code: python <scriptname>.py This option is slightly less convenient, but it may be necessary if you prefer to use a particular editor. You could also search for Python interpreter plugins for your editor. Atom Editor: https://atom.io/ Sublime Text: https://www.sublimetext.com/ VS Code: https://code.visualstudio.com/ |
AuthorAnything you need to know about computer science Archives
May 2023
Categories
All
|