Basics of Python — Hello World, Print Statements, If and For loops..
Pytho is a popular general-purpose programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
- web development (server-side).
- software development.
- Machine Learning.
- Deep Learning etc.
I have used Jupyter notebook for running the python scripts and version is 3.6. Hope you are aware of installing python and Jypyter notebooks. You can use other IDLEs which you are comfortable with. Lets dive into coding and start with the very basics.
The below code passes a constant string, containing the text “hello world” to a function that is named print.
We can also leave comments in our code to explain what we are doing. Comments can begin anywhere in a line and should begin with #.
Strings are very versatile and allow your program to process textual information. Constant string, enclosed in quotes, define literal string values inside your program. The triple quote allows for multiple lines of text.
In addition to strings, Python allows numbers as literal constants in programs. Python includes support for floating-point, integer, complex, and other types of numbers. This course will not make use of complex numbers. Unlike strings, quotes do not enclose numbers.
The following code assigns an integer value to a variable named “a” and a string value to a variable named “b.”
The key feature of variables is that they can change. The following code demonstrates how to change the values held by variables.
We can mix strings and variables for printing. This technique is called a formatted or interpolated string. The variables must be inside of the curly braces. In Python, this type of string is generally called an f-string. The f-string is denoted by placing an “f” just in front of the opening single or double quote that begins the string. The following code demonstrates the use of an f-string to mix several variables with a literal string.
You can also use f-strings with math (called an expression). Curly braces can enclose any valid Python expression for printing. The following code demonstrates the use of an expression inside of the curly braces of an f-string.
Python has many ways to print numbers; these are all correct. However, for this course, we will use f-strings. The following code demonstrates some of the varied methods of printing numbers in Python.
We can use if-statements to perform logic. These if-statements are how Python defines blocks of code to execute together. A block usually begins after a colon and includes any lines at the same level of indent. Unlike many other programming languages, Python uses whitespace to define blocks of code. The fact that whitespace is significant to the meaning of program code is a frequent source of annoyance for new programmers of Python. Tabs and spaces are both used to define the scope in a Python program. Mixing both spaces and tabs in the same program is not recommended.
The following if-statement has multiple levels. It can be easy to indent these levels improperly, so be careful. This code contains a nested if-statement under the first “a==5” if-statement. Only if a is equal to 5 will the nested “b==6” if-statement be executed. Also, not that the “elif” command means “else if.”
It is common in programming languages to loop over a range of numbers. Python accomplishes this through the use of the range operation. Here you can see a for loop and a range operation that causes the program to loop between 1 and 9.
The range command is used in conjunction with loops to pass over a specific range of numbers. Cases, where you must loop over specific number ranges, are somewhat uncommon. Generally, programmers use loops on collections of items, rather than hard-coding numeric values into your code. Collections, as well as the operations that loops can perform on them, is covered later in this module.
The following is a further example of a looped printing of strings and numbers.