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.
0 Comments
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/ Python Programming Language – Python IDLE Development Environment
We have a lot of options when it comes to our development environment. Using the default IDLE is the simplest option. It features an integrated interpreter and is a fantastic tool for developing code. Therefore, you can run the code immediately in IDLE. This is absolutely enough for beginners. You can just keep with the fundamental installation if you select this option. Python Programming Language - Python Installation
Now that we have installed Python and our development environment, let's get to the code. A so-called interpreted language is Python. This implies that in order to run the code, a specific piece of software (the interpreter) is required. Compiler-based languages include C++ and Java. Although a compiler is required, the programme is then transformed into machine code and can be run without additional software. Without a Python interpreter, it is impossible to run Python scripts. |
AuthorAnything you need to know about computer science Archives
May 2023
Categories
All
|