This document provides an introduction to Python basics, including mathematical expressions, operators, data types, strings, variables, user input, conditional statements, and flow control. Key concepts covered are entering expressions into the interactive shell, string concatenation and replication, storing values in variables, the print(), input(), len(), str(), int(), and float() functions, if, else, and elif statements. An example program demonstrates getting user input and printing output.
This document provides an introduction to Python basics, including mathematical expressions, operators, data types, strings, variables, user input, conditional statements, and flow control. Key concepts covered are entering expressions into the interactive shell, string concatenation and replication, storing values in variables, the print(), input(), len(), str(), int(), and float() functions, if, else, and elif statements. An example program demonstrates getting user input and printing output.
This document provides an introduction to Python basics, including mathematical expressions, operators, data types, strings, variables, user input, conditional statements, and flow control. Key concepts covered are entering expressions into the interactive shell, string concatenation and replication, storing values in variables, the print(), input(), len(), str(), int(), and float() functions, if, else, and elif statements. An example program demonstrates getting user input and printing output.
This document provides an introduction to Python basics, including mathematical expressions, operators, data types, strings, variables, user input, conditional statements, and flow control. Key concepts covered are entering expressions into the interactive shell, string concatenation and replication, storing values in variables, the print(), input(), len(), str(), int(), and float() functions, if, else, and elif statements. An example program demonstrates getting user input and printing output.
>>> 2 + 2 4 >>> 2 >>> 3*6 18 • You can try with the remaining mathematical functions. Math Operators Precedence Order of Operations Syntax Error Messages Integer, Floating Point and String Data Types String Concatenation • >>> 'Alice' + 'Bob’ 'AliceBob’ • >>> 'Alice' + 42 Traceback (most recent call last): File "", line 1, in 'Alice' + 42 TypeError: can only concatenate str (not "int") to str String Replication • >>> 'Alice' * 5 'AliceAliceAliceAliceAlice’ • >>> 'Alice' * 'Bob’ Traceback (most recent call last): File "", line 1, in 'Alice' * 'Bob’ TypeError: can't multiply sequence by non-int of type 'str' Storing Values in Variables Variable Names Sample Program • # This program says hello and asks for your name. • print('Hello, world!’) • print('What is your name?') # ask for their name • myName = input() • print('It is good to meet you, ' + myName) • print('The length of your name is:') print(len(myName)) • print('What is your age?') # ask for their age • myAge = input() • print('You will be ' + str(int(myAge) + 1) + ' in a year.') Dissecting Your Program • Comments • The print() Function • The input() Function • Printing the User’s Name • The len() Function • The str(), int(), and float() Functions Flow Chart Flow Control • Boolean Values • >>> spam = True >>> spam True • Comparison Operators Mixing Boolean and Comparison Operators Elements of Flow Control • if Statements • if name == 'Alice’: • print('Hi, Alice.’) • else Statements • if name == 'Alice’: • print('Hi, Alice.’) • else: • print('Hello, stranger.') Elements of Flow Control • elif Statements • if name == 'Alice’: • print('Hi, Alice.’) • elif age < 12: • print('You are not Alice, kiddo.’)