Python in easy way It includes different data type
2.
What is syntax? •Syntax implies the set of rules which define how the program will be written and interpreted. • For example, this is the Python syntax: • print (“Welcome, John!”)
3.
• In theabove example, syntax implies the characters such as opening and closing brackets () and the inverted commas “” as well as the keyword “print”. • Output: • Welcome, John!
4.
What is indentation? •Python uses indentation to identify the different blocks of code. • The white space indicates the indentation. • All the codes with the same spaces on the left belong to the same block of code. • Usually, the number of spaces on the left are four.
• Output: • 64 •In the example above, since the second line of code is a separate expression, the indentation is not correct. Hence, when you run the code it throws an error. But when you remove the space/indentation, the correct output is shown.
9.
What are variablesand operators? • Variables • Variables are characters or words that are given a user-defined value. • Variables, as the name suggests can be varied during the execution of the code. • They are allocated a space in the computer memory based on their data type. • Python doesn't require explicit declaration of the data type for a variable.
Unpack a Collection •If you have a collection of values in a list, tuple etc. Python allows you to extract the values into variables. This is called unpacking.
21.
Output Variables • ThePython print() function is often used to ou • tput variables • x = "Python is awesome" print(x) • Output • Python is awesome
22.
• In theprint() function, you output multiple variables, separated by a comma: • x = "Python" y = "is" z = "awesome" print(x, y, z) • Output • Python is awesome
23.
• You canalso use the + operator to output multiple variables: • x = "Python " y = "is " z = "awesome" print(x + y + z) • Output • Python is awesome
24.
• For numbers,the + character works as a mathematical operator: • x = 5 y = 10 print(x + y) • Output • 15
25.
• In theprint() function, when you try to combine a string and a number with the + operator, Python will give you an error: • x = 5 y = "John" print(x + y)
• The bestway to output multiple variables in the print() function is to separate them with commas, which even support different data types: • x = 5 y = "John" print(x, y) • Output • 5 John
28.
Global Variables • Variablesthat are created outside of a function (as in all of the examples above) are known as global variables. • Global variables can be used by everyone, both inside of functions and outside. • Example • Create a variable outside of a function, and use it inside the function • x = "awesome" def myfunc(): print("Python is " + x) myfunc()
• If youcreate a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
31.
• Create avariable inside a function, with the same name as the global variable • x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
The global Keyword •Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. • To create a global variable inside a function, you can use the global keyword.
34.
• If youuse the global keyword, the variable belongs to the global scope: • def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
• Also, usethe global keyword if you want to change a global variable inside a function. • Example • To change the value of a global variable inside a function, refer to the variable by using the global keyword: • x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x)
Exercise • Create avariable named carname and assign the value Volvo to it. • Create a variable named x and assign the value 50 to it. • Display the sum of 5 + 10, using two variables: x and y. • Create a variable called z, assign x + y to it, and display the result